背景脚本:
let msg = 'ty for help';
chrome.browserAction.onClicked.addListener(function (event) {
console.log('clicked');
chrome.tabs.executeScript(null, {
file: 'js/content.js', /* my content script */ }, () => {
connect(msg) //this is where I call my function to establish a connection
});
});
});
function connect(msg) {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const port = chrome.tabs.connect(tabs[0].id);
port.postMessage(msg);
});
}内容脚本:
chrome.runtime.onConnect.addListener(function(port) {
port.onMessage.addListener(function(show_floater) {
console.log('why do I get printed more than once per click?????');
});
});我想要的行为是,每当用户点击浏览器的扩展图标时,我的内容脚本的代码就会执行一次。从现在开始,每次点击它都会被调用多次。Here,在端口生存期下,它说可以为所有帧调用onConnect。我试图在清单中将all_frames设置为false,但没有任何变化...发送帮助D:
发布于 2020-11-25 01:14:10
我在清单中有内容脚本,我也会通过上面的代码注入它……
https://stackoverflow.com/questions/64988863
复制相似问题