Google chrome extension 从后台脚本修改后台页面
我有这个分机 manifest.json:Google chrome extension 从后台脚本修改后台页面,google-chrome-extension,Google Chrome Extension,我有这个分机 manifest.json: { "manifest_version": 2, ... "background_page": "background.html", "background" : { "scripts": ["main.js"] }, "permissions":
{
"manifest_version": 2,
...
"background_page": "background.html",
"background" : {
"scripts": ["main.js"]
},
"permissions": ["activeTab"],
"browser_action": {}
}
main.js:
chrome.browserAction.onClicked.addListener(
function(tab) {
chrome.tabs.executeScript(null, { file: "jquery-3.5.1.js" }, function() {
chrome.tabs.executeScript(null, { file: "doit.js" });
});
}
);
doit.js:
try{
const getReq = $.get("...");
getReq.done(function(data){
//modify divs values from `background.html`
console.log("done");
});
getReq.fail(function(req, status, error){
console.log(status);
console.log(error);
});
}catch(e){ console.log(e); }
background.html:
<html>
<body>
<div id="data"></div>
</body>
</html>
如何从
doit.js
修改background.html
。后台页面是一个单独的隐藏页面,后台脚本在其中运行,它不能直接从内容脚本(doit.js)访问,只能通过无法传输DOM元素的消息传递来访问。应该不需要在后台脚本中使用DOM,它的用途是不同的。此外,现代Chrome不允许跨源代码$。进入内容脚本:在后台脚本中执行。