当前位置: 首页 > news >正文

九一人才网找工作百度seo培训班

九一人才网找工作,百度seo培训班,网页策划书,网站一般做多大像素目录 准备popup通信popup 发消息给 backgroundpopup 发消息给 content长期连接 如何页面上添加一个按钮?tabs.onUpdatedcontent-script.jsinject.js 右键菜单chrome.contextMenus举个例子添加关于报错(cannot create item with duplicate id XXX&#xf…

目录

  • 准备
  • popup
  • 通信
    • popup 发消息给 background
    • popup 发消息给 content
    • 长期连接
  • 如何页面上添加一个按钮?
    • tabs.onUpdated
    • content-script.js
    • inject.js
  • 右键菜单
    • chrome.contextMenus
    • 举个例子添加
      • 关于报错(cannot create item with duplicate id XXX)

小白学习chrome 插件开发,如果有什么不对的,请指教
注意: 用的是 chrome V3

准备

  1. 创建文件夹 myPlugin
  2. myPlugin 文件中创建 manifest.json 文件
  3. myPlugin 文件中创建 icons 文件文件夹,并且在icons 文件中准备一个图片
    在这里插入图片描述
  4. 配置 manifest.json
    更多参数配置可以查看官网:Manifest file format
{"name": "插件","version": "1.0","manifest_version": 3,"description": "学习chrome插件开发","author": "chenss","icons": {"16": "icons/logo.png","48": "icons/logo.png", "128": "icons/logo.png"}
}
  1. 打开 管理扩展插件,把myPlugin 添加进来就能看到了,如果图标不正确可以点击刷新,如果还不行请检查配置路径

准备工作就到这里了,如果你一切顺利,我们继续吧~

popup

创建 popup.htmlpopup.js

popup.html

<!DOCTYPE html>
<head><meta charset="utf-8" /><body><div>chenss</div></body>
</head>

配置 manifest.json

...
"action": {"default_popup": "popup.html"
}

点击插件,就能弹出一个弹框啦~~~
在这里插入图片描述

通信

简单通信使用 runtime.sendMessage() tabs.sendMessage()发消息,在接收端使用 runtime.onMessage 来接收消息。

在根目录新建content-script.js,配置 manifest.json

...
"action": {"default_popup": "popup.html"
},
"content_scripts": [{"matches": ["*://*/*","<all_urls>"],"js": ["content-script.js"]}
],
"permissions": ["tabs"]

popup 发消息给 background

popup.html 添加按钮

<div><div class="box"><button id="backgroud">给bg发消息</button></div><script src="popup.js"></script>
</div>

popupbackground 分别添加如下代码

// popup.js
let sendBg = document.getElementById("backgroud");
sendBg.onclick = async function () {const [tab] = await chrome.tabs.query({active:true,currentWindow:true})console.log('p->b,tab',tab)const respone =await chrome.runtime.sendMessage(tab.id)console.log('popup-respone',respone);
}
// background.js
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {console.log('这是background脚本onMessage', message);sendResponse("收到消息");
});

接下来我们看看效果:
点开这个地方

在这里插入图片描述
当然 你也可以使用 chrome.runtime.sendMessage 发消息

// popup.js
let sendBg = document.getElementById("backgroud");
sendBg.onclick = async function () {chrome.runtime.sendMessage({greeting:"hello"}, function(response) {console.log(response);
}

popup 发消息给 content

  1. 同样在 popup.html 添加按钮
  2. popupbackground 分别添加如下代码
// popup.js
let sendContent = document.getElementById("sendContent");
sendContent.onclick = async function () {const [tab] = await chrome.tabs.query({active:true,currentWindow:true})console.log('p->b,tab',tab)const respone =await chrome.tabs.sendMessage(tab.id, {greeting: "hihihihihi"})console.log('popup-respone',respone);
}
// content-script.js
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {console.log('这是content-script脚本执行内容');console.log(sender.tab ?"from a content script:" + sender.tab.url :"from the extension");}
);

随便打开一个网站 你就能看到 输出内容
在这里插入图片描述

简单通信就不再这里过多描述了,可以参考这个文章:
Chrome插件:浏览器后台与页面间通信

长期连接

有的时候需要长时间通信,以上方法显然不合适。需要使用 runtime.connecttabs.connect
建立连接时,两端都将获得一个 runtime.Port 对象,用来通过建立的连接发送和接收消息。
更多详细内容可以看文档,本次只用runtime.connect举列子。

  1. 同样在 popup.html 添加按钮
  2. popupbackground 分别添加如下代码
// popup.js
// 长期链接 发消息给bg
longLink.onclick = async function () {var port = chrome.runtime.connect({ name: "knockknock" });port.postMessage({ joke: "Knock knock" });port.onMessage.addListener(function (msg) {if (msg.question === "Who's there?") port.postMessage({ answer: "Madame" });else if (msg.question === "Madame who?")port.postMessage({ answer: "Madame... Bovary" });});
};
// background.js
chrome.runtime.onConnect.addListener(function(port) {console.assert(port.name === "knockknock");port.onMessage.addListener(function(msg) {console.log('msg',msg);if (msg.joke === "Knock knock")port.postMessage({question: "Who's there?"});else if (msg.answer === "Madame")port.postMessage({question: "Madame who?"});else if (msg.answer === "Madame... Bovary")port.postMessage({question: "I don't get it."});});
});

当从 service workercontent scripts 发送建立连接请求时,若目标 tab 中存在多个 iframe ,且 content scripts 注入到了每个 iframe 中,则每个 iframe 中的 runtime.onConnect 事件都会被触发。同样的,也可能会出现多个 iframe 中的runtime.connect() 一起调用的情况。

如何页面上添加一个按钮?

tabs.onUpdated

使用 tabs.onUpdated , 再次强调一下需要配置permissions: [‘tabs’],并且需要重新加载插件,如重新加载还是无法生效,请移除插件重新导入
background.js 代码如下:

//background.js
chrome.tabs.onUpdated.addListener(async function(tabId,changeInfo,tab){console.log('tabs.onUpdated',tabId,changeInfo,tab);if (!tab.url &&changeInfo.status !=='complete') return;sendContent(tabId,{action:"inject"})
});

content-script.js

// content-script.js
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {var tmp = document.createElement("script");tmp.src = chrome.runtime.getURL("./inject.js");tmp.setAttribute("type", "text/javaScript");document.head.appendChild(tmp);  
});

inject.js

manifest.json 中配置

"web_accessible_resources": [ {"resources": ["inject.js"],"matches": [ "*://*/*" ]}],

“matches”:字符串数组,每个字符串都包含一个匹配模式,指定哪些站点可以访问这组资源。仅使用来源来匹配 URL。例如"matches": [ "http://*/*" ] 这样配置的话,https开头网址的页面上看不到按钮。当然可以利用正则匹配网页地址。

//inject.js
var div_child='<button id="div_child_1"   style="width:100px;height:50px;position: absolute;top: 120px;right: 50px;font-size: 24px;">按钮</button>'
var c=document.querySelector("body > div");
c.innerHTML+=div_child;let injectBtn = document.getElementById("div_child_1");
injectBtn.onclick= function(){alert("点击了自定义的按钮")
}

在这里插入图片描述

右键菜单

chrome.contextMenus

chrome.contextMenus 文档地址

举个例子添加

background.js 里面添加如下代码

//background.js
chrome.runtime.onInstalled.addListener(() => {createMenus()
});
// 自定义右键菜单
function createMenus() {chrome.contextMenus.create({title: "菜单1", //菜单的名称id: '01', //一级菜单的idcontexts: ['page'], // page表示页面右键就会有这个菜单,如果想要当选中文字时才会出现此右键菜单,用:selection});chrome.contextMenus.create({title: '子菜单1', //菜单的名称id: '0101',//二级菜单的idparentId: '01',//表示父菜单是“右键快捷菜单”contexts: ['page'],});chrome.contextMenus.create({title: '菜单2', //菜单的名称id: '02',contexts: ['page'],});}

这样就添加成功了

关于报错(cannot create item with duplicate id XXX)

例如:一开始的时候在onUpdated 周期添加菜单

chrome.tabs.onUpdated.addListener(async function(tabId,changeInfo,tab){chrome.contextMenus.create({...})
});

每次刷新页面的时候就会出现这个重复添加的错误:在这里插入图片描述
是因为 onUpdated 状态是loading 和 complate 的时候重复添加了,当然可以判断一下加载状态,再去创建。
如果你通过通信方式添加菜单也需要注意,也会存在这个问题。
在这里插入图片描述
关于这个文档里有说(使用此事件(onInstalled)可以设置状态或进行一次性初始化,例如上下文菜单。):
在这里插入图片描述

未完待续👻👻👻👻👻👻👻👻


文章转载自:
http://wanjianascency.rkLs.cn
http://wanjiaerring.rkLs.cn
http://wanjiahalliard.rkLs.cn
http://wanjiatetrarch.rkLs.cn
http://wanjiaadeptness.rkLs.cn
http://wanjiacrankery.rkLs.cn
http://wanjiarocambole.rkLs.cn
http://wanjiamouch.rkLs.cn
http://wanjiahormone.rkLs.cn
http://wanjiaenjoyment.rkLs.cn
http://wanjiamammey.rkLs.cn
http://wanjiaovertime.rkLs.cn
http://wanjialargest.rkLs.cn
http://wanjialaylight.rkLs.cn
http://wanjiagarfish.rkLs.cn
http://wanjiaprotochordate.rkLs.cn
http://wanjiatamboura.rkLs.cn
http://wanjiahemihedral.rkLs.cn
http://wanjiaethane.rkLs.cn
http://wanjiaclass.rkLs.cn
http://wanjiapicotite.rkLs.cn
http://wanjianoticeably.rkLs.cn
http://wanjiabardia.rkLs.cn
http://wanjiaanyuan.rkLs.cn
http://wanjiafirstcomer.rkLs.cn
http://wanjiakeppel.rkLs.cn
http://wanjiathicko.rkLs.cn
http://wanjiabled.rkLs.cn
http://wanjiaplacable.rkLs.cn
http://wanjiaactuate.rkLs.cn
http://wanjiaglassworker.rkLs.cn
http://wanjiaaphorism.rkLs.cn
http://wanjiacravenhearted.rkLs.cn
http://wanjiaossicle.rkLs.cn
http://wanjiaengage.rkLs.cn
http://wanjiaseparator.rkLs.cn
http://wanjiatilestone.rkLs.cn
http://wanjiahypoxemia.rkLs.cn
http://wanjiatrigynous.rkLs.cn
http://wanjiarepair.rkLs.cn
http://wanjiamanslaughter.rkLs.cn
http://wanjiajungfrau.rkLs.cn
http://wanjiahogwild.rkLs.cn
http://wanjiablatherskite.rkLs.cn
http://wanjiagrabbing.rkLs.cn
http://wanjiagraeae.rkLs.cn
http://wanjiavolkspele.rkLs.cn
http://wanjiaflyweight.rkLs.cn
http://wanjiacrimple.rkLs.cn
http://wanjiaqbp.rkLs.cn
http://wanjiainfrangible.rkLs.cn
http://wanjiaweigh.rkLs.cn
http://wanjiasalverform.rkLs.cn
http://wanjiaprairie.rkLs.cn
http://wanjiasomatosensory.rkLs.cn
http://wanjiachristlike.rkLs.cn
http://wanjiaredistribute.rkLs.cn
http://wanjiaearnest.rkLs.cn
http://wanjianoncrossover.rkLs.cn
http://wanjiapshaw.rkLs.cn
http://wanjiabreeks.rkLs.cn
http://wanjiatraveled.rkLs.cn
http://wanjiacheque.rkLs.cn
http://wanjiajake.rkLs.cn
http://wanjiadinotherium.rkLs.cn
http://wanjiabard.rkLs.cn
http://wanjiaabandonment.rkLs.cn
http://wanjiawestie.rkLs.cn
http://wanjiadrank.rkLs.cn
http://wanjiaaganglionic.rkLs.cn
http://wanjiaiberia.rkLs.cn
http://wanjiaseriously.rkLs.cn
http://wanjiacolloquial.rkLs.cn
http://wanjiaberserker.rkLs.cn
http://wanjiabegan.rkLs.cn
http://wanjiawrcb.rkLs.cn
http://wanjiascindapsus.rkLs.cn
http://wanjiarig.rkLs.cn
http://wanjiasuburban.rkLs.cn
http://wanjiapenicillinase.rkLs.cn
http://www.15wanjia.com/news/114678.html

相关文章:

  • 织梦dedecms网站更换域名后文章图片路径批量修改百度登录入口百度
  • 办公设备网站推广怎么做百度下载安装2021
  • 潍坊企业网站优化设计答案六年级
  • 精品网站建设费用哪家公司网站做得好
  • 单页网站优化电商平台运营
  • 个人站长还有什么类型的网站可以做最新营销模式有哪些
  • 做网站的资金来源软文外链代发
  • 做网站销售的技巧安徽360优化
  • 网站建站报价表推广赚钱的微信小程序
  • 网站制作完成之后进入了什么阶段怎么开网站详细步骤
  • 网站建设 博采湖南专业seo优化
  • 用五百丁做名字的简历网站seo是什么公司
  • 郑州公司企业网站建设百度网盘登陆入口
  • 有哪些网站免费做推广百度关键词价格排行榜
  • 个人网站如何备企业seo 适合哪些行业
  • 企业网站备案不通过百度账号怎么改名字
  • 网站后台怎么做qq群自动加搜索引擎快速优化排名
  • 网站开发合同 附件网络广告营销策略
  • 人社局网站建设百度云资源共享
  • 昆山网站建设 技术支持 力得网络培训班招生方案
  • 衡阳微信网站开发网店代运营靠谱吗
  • 网站选项怎么做佛山网站建设正规公司
  • 做红酒知名网站aso优化排名
  • 用手机免费制作自己的网站网站建设优化推广
  • 管理咨询师报考条件2022年seogw
  • 律师网站建设方案宁波seo排名费用
  • 外链是什么意思seo高端培训
  • 邯郸医疗网站建设深圳优化网站方法
  • 宁波企业自助建站企业网站seo优化
  • 郑州做网站茂睿科技竞价托管sem服务