假设我想注入一些代码来在 chrome 的新窗口上创建一个按钮(窗口本身是通过扩展的 popup.js 打开的)。我该怎么做?
答案 0 :(得分:1)
你可以在foreground.js中使用诸如documnet.body.appendChild()之类的函数而不是console.log()在打开文件并以我给它们命名的方式填充它们之后。您可以通过这种方式向页面添加任何您想要的内容。
背景.js
function as(a,b,c) {
chrome.tabs.executeScript(null, {file: "foreground.js"});
chrome.tabs.insertCSS(null,{file:'./style.css'})
console.log(a,b,c+'asd')
}
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
as(tabId, changeInfo, tab);
});
前景.js
window.onload=()=>{
console.log('ok')
}
manifest.json
{
"name": "plugin",
"version": "1.0",
"manifest_version":2,
"background": {
"scripts":["./background.js"]
},
"browser_action":{
"default_popup":"popup.html"
},
"permissions":[
"tabs",
"https://*/*"
]
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
</body>
</html>
style.css
button{
color:red;
}