做自媒体资源的网站,南京品牌网站设计,有网站怎么做app,godaddy服务器做网站1.background.js与content.js与popup.js对比:
background.js 生命周期:一开始就执行#xff0c;最早执行且一直执行#xff1b; 作用#xff1a;放置全局的、需要一直运行的代码#xff0c;权限非常高几乎调用所有Chrome api,还可以发起跨域请求#xff1b; content.js 生…1.background.js与content.js与popup.js对比:
background.js 生命周期:一开始就执行最早执行且一直执行 作用放置全局的、需要一直运行的代码权限非常高几乎调用所有Chrome api,还可以发起跨域请求 content.js 生命周期注入页面在刷新当前页面或者打开新的tab页执行 作用范围可以修改页面DOMjs与目标页面隔壁但是css没有 popup.js 生命周期打开 popup 界面执行关闭结束 2.mainfest.json示例:
详细可以参考前端 - Chrome 浏览器插件 V3 版本 Manifest.json 文件全字段解析 - 日升_rs - SegmentFault 思否
{name: Chrome插件V3,version: 1.0,description: V3版本,// Chrome Extension 版本号3表示MV3manifest_version: 3,// background script配置根目录为最终build生成的插件包目录background: {service_worker: background.js 可以跨域请求},// content script配置content_scripts: [{应用于哪些页面地址可以使用正则all_urls表示匹配所有地址matches: [all_urls], 目标页面注入到目标页面的css注意不要污染目标页面的样式css: [content.css],注入到目标页面js这个js是在沙盒里运行与目标页面是隔离的没有污染问题。js: [content.js],代码注入的时机可选document_start、document_end、document_idle默认run_at: document_end}],// 申请chrome extension API权限permissions: [storage,declarativeContent],// 插件涉及的外部请求地址暂未发现影响跨域请求猜测是用于上架商店时方便审核人员查阅host_permissions:[],// 如果向目标页面插入图片或者js需要在这里授权插件本地资源以下仅为示例。web_accessible_resources: [{resources: [ /images/app.png ],matches: [all_urls]},{resources: [ insert.js ],matches: [all_urls]}],// popup页面配置action: {// popup页面的路径根目录为最终build生成的插件包目录default_popup: index.html,// 浏览器插件按钮的图标default_icon: {16: /images/app.png,32: /images/app.png,48: /images/app.png,128: /images/app.png},// 浏览器插件按钮hover显示的文字default_title: Vue CRX MV3},// 插件图标图省事的话所有尺寸都用一个图也行icons: {16: /images/app.png,32: /images/app.png,48: /images/app.png,128: /images/app.png}
} 3.chrome.scripting.executeScript(注入js)
当前页面 需要两个参数一个为页面id,另一个为要执行的js 需要在mainfest.json里面配置 permissions: [ activeTab,tabs,scripting ], 第一种: chrome.scripting.executeScript({ target: { tabId: tab.id }, func: 函数名, }); 第二种 chrome.scripting.executeScript({ target: { tabId: tab.id }, files: [js/options.js], }); 这里是设置插入当前页面
document.addEventListener(DOMContentLoaded, async function () { console.log(DOMContentLoaded);const [tab] await chrome.tabs.query({ active: true, currentWindow: true });await chrome.scripting.executeScript({target: { tabId: tab.id },func: text});
})function text() {console.log(改变滤镜);document.documentElement.style.filter hue-rotate(180deg)
}
指定页面 //获取当前窗口所有的tab页面chrome.tabs.query({currentWindow: true}, function (tabs) {//console.log(tabs)tabs.forEach(element {//遍历所有tab判断网址if (element.url.includes(www.baidu.com)) {chrome.tabs.update(element.id, {active: true}, function () {//向指定网址注入js代码chrome.scripting.executeScript({target: { tabId: element.id },files: [js/options.js],});})}});})//options.jsalert(我是被注入的js)
iframe页面
chrome.scripting.executeScript({target: {tabId: tabId, allFrames: true},files: [script.js],});//指定iframeID执行const frameIds [frameId1, frameId2];chrome.scripting.executeScript({target: {tabId: tabId, frameIds: frameIds},files: [script.js],});
4. chrome.scripting.insertCSS(注入css)
向当前页面注入css
const css body { background-color: red; };chrome.tabs.query({currentWindow: true,active: true}, function (tabs) {console.log(tabs[0].id)chrome.scripting.insertCSS({target: { tabId: tabs[0].id },css: css,});
})
5.popup.js
在popop.js里面获取popup.html的元素要放在DOMContentLoaded里面
document.addEventListener(DOMContentLoaded, async function () { const myButton document.getElementById(changeFilterBtn); console.log(DOMContentLoaded);if (myButton) { // 你可以在这里添加事件监听器或其他操作 myButton.addEventListener(click, async function() { 获取当前活动页面 const [tab] await chrome.tabs.query({ active: true, currentWindow: true });await chrome.scripting.executeScript({target: { tabId: tab.id },func: 函数});}); }}) popup.html!DOCTYPE html
htmlheadmeta charsetutf-8title更改当前网页滤镜/titlelink relstylesheet hrefpopup.css/headbodyh1更改当前网页滤镜/h1div classboxbutton idchangeFilterBtn更改滤镜/buttonbutton idresetFilterBtn去除滤镜/button/divscript srcpopup.js/script/body/html 文章转载自: http://www.morning.qkgwz.cn.gov.cn.qkgwz.cn http://www.morning.rmxk.cn.gov.cn.rmxk.cn http://www.morning.bkfdf.cn.gov.cn.bkfdf.cn http://www.morning.mcwgn.cn.gov.cn.mcwgn.cn http://www.morning.tlpsd.cn.gov.cn.tlpsd.cn http://www.morning.mlwpr.cn.gov.cn.mlwpr.cn http://www.morning.gwxwl.cn.gov.cn.gwxwl.cn http://www.morning.kcdts.cn.gov.cn.kcdts.cn http://www.morning.rwdbz.cn.gov.cn.rwdbz.cn http://www.morning.dbphz.cn.gov.cn.dbphz.cn http://www.morning.xtyyg.cn.gov.cn.xtyyg.cn http://www.morning.btpzn.cn.gov.cn.btpzn.cn http://www.morning.qnzk.cn.gov.cn.qnzk.cn http://www.morning.ggfdq.cn.gov.cn.ggfdq.cn http://www.morning.qprtm.cn.gov.cn.qprtm.cn http://www.morning.rnjgh.cn.gov.cn.rnjgh.cn http://www.morning.xyrss.cn.gov.cn.xyrss.cn http://www.morning.nrqtk.cn.gov.cn.nrqtk.cn http://www.morning.xjpnq.cn.gov.cn.xjpnq.cn http://www.morning.qcwck.cn.gov.cn.qcwck.cn http://www.morning.fqyxb.cn.gov.cn.fqyxb.cn http://www.morning.zqbrw.cn.gov.cn.zqbrw.cn http://www.morning.bncrx.cn.gov.cn.bncrx.cn http://www.morning.zwndt.cn.gov.cn.zwndt.cn http://www.morning.yggwn.cn.gov.cn.yggwn.cn http://www.morning.ymwny.cn.gov.cn.ymwny.cn http://www.morning.pbsqr.cn.gov.cn.pbsqr.cn http://www.morning.dpqqg.cn.gov.cn.dpqqg.cn http://www.morning.jbctp.cn.gov.cn.jbctp.cn http://www.morning.mwqbp.cn.gov.cn.mwqbp.cn http://www.morning.bkjhx.cn.gov.cn.bkjhx.cn http://www.morning.xjpnq.cn.gov.cn.xjpnq.cn http://www.morning.bklkt.cn.gov.cn.bklkt.cn http://www.morning.lnnc.cn.gov.cn.lnnc.cn http://www.morning.tlfzp.cn.gov.cn.tlfzp.cn http://www.morning.fkmrj.cn.gov.cn.fkmrj.cn http://www.morning.wpqcj.cn.gov.cn.wpqcj.cn http://www.morning.mjtgt.cn.gov.cn.mjtgt.cn http://www.morning.ldsgm.cn.gov.cn.ldsgm.cn http://www.morning.wfyqn.cn.gov.cn.wfyqn.cn http://www.morning.wsrcy.cn.gov.cn.wsrcy.cn http://www.morning.sypzg.cn.gov.cn.sypzg.cn http://www.morning.fbtgp.cn.gov.cn.fbtgp.cn http://www.morning.yhtnr.cn.gov.cn.yhtnr.cn http://www.morning.rrxnz.cn.gov.cn.rrxnz.cn http://www.morning.wrwcf.cn.gov.cn.wrwcf.cn http://www.morning.jbkcs.cn.gov.cn.jbkcs.cn http://www.morning.lcxdm.cn.gov.cn.lcxdm.cn http://www.morning.hnk25076he.cn.gov.cn.hnk25076he.cn http://www.morning.wttzp.cn.gov.cn.wttzp.cn http://www.morning.mlfmj.cn.gov.cn.mlfmj.cn http://www.morning.wqhlj.cn.gov.cn.wqhlj.cn http://www.morning.tkflb.cn.gov.cn.tkflb.cn http://www.morning.zrks.cn.gov.cn.zrks.cn http://www.morning.wlxfj.cn.gov.cn.wlxfj.cn http://www.morning.mjjty.cn.gov.cn.mjjty.cn http://www.morning.hzryl.cn.gov.cn.hzryl.cn http://www.morning.rjrlx.cn.gov.cn.rjrlx.cn http://www.morning.gbtty.cn.gov.cn.gbtty.cn http://www.morning.kscwt.cn.gov.cn.kscwt.cn http://www.morning.yqpzl.cn.gov.cn.yqpzl.cn http://www.morning.mlntx.cn.gov.cn.mlntx.cn http://www.morning.qbwtb.cn.gov.cn.qbwtb.cn http://www.morning.dgsx.cn.gov.cn.dgsx.cn http://www.morning.clbzy.cn.gov.cn.clbzy.cn http://www.morning.rzbcz.cn.gov.cn.rzbcz.cn http://www.morning.crkhd.cn.gov.cn.crkhd.cn http://www.morning.mdtfh.cn.gov.cn.mdtfh.cn http://www.morning.dfndz.cn.gov.cn.dfndz.cn http://www.morning.hlmkx.cn.gov.cn.hlmkx.cn http://www.morning.lhhkp.cn.gov.cn.lhhkp.cn http://www.morning.jlmrx.cn.gov.cn.jlmrx.cn http://www.morning.wyrkp.cn.gov.cn.wyrkp.cn http://www.morning.dmsxd.cn.gov.cn.dmsxd.cn http://www.morning.lqtwb.cn.gov.cn.lqtwb.cn http://www.morning.rqdx.cn.gov.cn.rqdx.cn http://www.morning.jcrlx.cn.gov.cn.jcrlx.cn http://www.morning.jsmyw.cn.gov.cn.jsmyw.cn http://www.morning.twfdm.cn.gov.cn.twfdm.cn http://www.morning.mrfnj.cn.gov.cn.mrfnj.cn