首页 > 其他分享 >Chrome字体插件

Chrome字体插件

时间:2023-11-01 20:35:41浏览次数:27  
标签:function 插件 Chrome style 字体 let font fontId

FontsChanger字体插件可以强制修改字体, 但是一些等宽字体被替换后效果不好, 可手动修改代码

background.js

const getFont = () => new Promise(ok => chrome.fontSettings.getFont({ genericFamily: "standard"}, ({fontId}) => ok(fontId)));
const getFontFix = () => new Promise(ok => chrome.fontSettings.getFont({ genericFamily: "fixed"}, ({fontId}) => ok(fontId)));

....

function injectFont(tabId, fontId, fontIdFix){
	try{
		const code = 
			"if(typeof UpdateFontBody === 'function')" + 
			"  UpdateFontBody(" + JSON.stringify(chrome.runtime.id) + ", " + JSON.stringify(fontId) + ", " + JSON.stringify(fontIdFix) + ");";
		chrome.tabs.executeScript(tabId, {code,		});
	}catch(e){}
}

content.js

var		font_main="";
var		font_fixed="";


function isEmpty(value) 
{
  return typeof value === 'undefined' || value === null;
}

function GetAllStyle(callback, param)
{
	let 	ets;
	let 	et, rus, sty;
	let		shts;
	let 	i, j;
		
	shts = document.styleSheets;
	for(i=0; i<shts.length; i++)
	{
		rus = shts[i].cssRules;
		if(isEmpty(rus))
			continue;
		for(j=0; j<rus.length; j++)
		{
			sty = rus[j].style;
			if(sty != null)
				callback(sty, param);
		}
	}
}

function strRemoveChar(str, ch) 
{
  return str.split('').filter(
	  function (c) 
	  {
	    return c !== ch;
	  }).join('');
}

function GetFontMain(fontfm)
{
	let		fm1, fm2, sf;
	
	if(fontfm.indexOf("var") != -1)
		return "";
	if(fontfm.indexOf("inherit") != -1)
		return "";
	
	fm1 = strRemoveChar(fontfm, '"');
	sf = fm1.split(',');
	fm2 = sf ? sf[0] : fm1;
	return fm2;
}

function GetNewFont(fontfm)
{
	let		fmnew, fmold;
	let		i;
	let		fixlist = ["courier", "courier new", "consolas", "fixedsys", "monospace"];
	let		comlist = ["arial", "宋体", "tahoma"];
	
	fmold = GetFontMain(fontfm);
	if(fmold == "")
		return "";
	
	fmold = fmold.toLowerCase();
	for(i=0; i<fixlist.length; i++)
	{
		if(fixlist[i] == fmold)
			return font_fixed;
	}
	for(i=0; i<comlist.length; i++)
	{
		if(comlist[i] == fmold)
			return font_main;
	}

	return "";
}

function StyleProc(sty, param)
{
	let 	fontfm, fmnew;
	
	if(isEmpty(sty))
		return;
	
	fontfm = sty.fontFamily;
	if(!fontfm)
		return;

	console.log("fontFamily: " + fontfm);
	fmnew = GetNewFont(fontfm);
	if(fmnew == "")
	{
		console.log("fontFamily: " + fontfm);
		return;
	}
	
	sty.fontFamily = fmnew;
	console.log("fontFamily: " + fontfm + "   ->    " + fmnew);
}
	
function UpdateFontBody(stid, fontMain, fontFix)
{
	let style = document.querySelector(stid);
	if(!style){
		style = document.createElement("style");
		style.id = stid;
		document.head.appendChild(style);
	}
	let st_ext = `
    html, body{font-family: '${fontMain}'!important;}
    *:not([class*='fa-']){font-family: '${fontMain}';}
`;
	font_main = fontMain;
	font_fixed = fontFix;
	
	if(0)	//	强制用固定字体
	{
		style.innerHTML = st_ext;
	}
	else
	{
		GetAllStyle(StyleProc, this);
	}
}

标签:function,插件,Chrome,style,字体,let,font,fontId
From: https://www.cnblogs.com/zzz3265/p/17804027.html

相关文章

  • Python selenium Chrome下载文件并设置下载路径
    PythonseleniumChrome下载文件并设置下载路径具体代码如下:importosimporttimefromtimeimportsleepfromseleniumimportwebdriverfromselenium.webdriver.common.byimportBydown_path="D:\\Temp"chrome_options=webdriver.ChromeOptions()diy_prefs={......
  • VS Qt扩展插件下载地址
      使用vs开发qt项目,需要安装qt插件QT插件下载地址:https://mirrors.ustc.edu.cn/qtproject/official_releases/vsaddin/ ......
  • Python使用selenium的Chrome下载文件报错解决
    Python使用selenium的Chrome下载文件报错:失败下载错误。网络不稳定也会引发该错误。咱们这里是因为路径多个反斜杠造成的。 下图是报错内容运行日志:路径代码:base_url="https://www.2ppt.com/"#采集的网址ASP.NET电子商务源码save_path="E:\\Spider\\PPT\\"去掉SaveP......
  • 【2023-11-01】一款基于 pdf.js 的 PDF 批注注释插件库(纯JS、高亮、画笔、多边形、历
    基于纯JavaScript和PDF.js做的一款PDF批注拓展插件-PDFMaster,一款仍能兼容支持IE11的PDF批注插件,界面美观功能强大,有无开发经验都可以快速简单快速使用。Demo及源码Demo和源码地址:https://demos.libertynlp.com功能演示视频:https://www.bilibili.com/video/BV12C4y1n7TL......
  • 分页插件
    我个人觉得这个东西有点麻烦,下面是过程的一些笔记 ......
  • oh my zsh 常用插件
     官方插件在这里,可以查看每个插件的具体使用方法。Plugins·ohmyzsh/ohmyzshWiki·GitHub下面提供几个常用的插件:git:提供了许多与Git相关的别名和功能。z:让你快速跳转到最近或经常使用的目录。autojump:一个更加强大的目录跳转工具。autosuggestions:根据你的命令历史......
  • Chrome浏览器查看前端参数
    按F12或者右键点击“检查”进入开发者模式。点击这个箭头图标,即可选择要查看参数的区域。 查看表格的行高和列宽当鼠标移到某区域,就会出现浮层,显示此区域的长和宽 查看字体和字号在搜索栏搜索关键字“font-size”可找到字号 ......
  • chrome扩展-FontsChanger等
    chrome替换网站的字体,各种网站使用的字体不一样,WIN10如果没这个字体就会用宋体,非常难看方法有很多写dll插件,自动替换API的宋体为雅黑按网站字体优先,复制一个雅黑字体修改名称,然后再安装,但是不同网站不一样要改比较多直接修改系统的宋体,这个很麻烦,这个方法......
  • 字体库加密
    自定义字体库工具:Fontmin(http://ecomfe.github.io/fontmin/#banner)常用汉字集:https://gitee.com/feng_xingkai/chinese1、打开Fontmin,将自定义字体库需要的字粘贴到页面中,并拖入字体文件,然后点击生成,获得自定义的字体库。修改字体映射关系fontTools安装:pipinstallfontToo......
  • k8s及其基本插件安装
    k8s_install.sh#!/bin/bashset-emaster1="10.1.1.60"master2="10.1.1.61"master3="10.1.1.62"containerd_version=v1.7.7runc_version=v1.1.9cni_version=v1.3.0cri_tools_version=v1.27.1kubernetes_version=v1.27.7kubernete......