Chrome 扩展:如何将code代码注入新的window
Chrome Extension: How to inject code code into a new window
所以假设我想注入一些代码以在 chrome 中的新 window 上创建一个按钮(window 本身是通过扩展程序的 popup.js 打开的).我该怎么做?
您可以在 foreground.js 中使用 documnet.body.appendChild() 之类的函数代替 console.log() ,然后用我给出的名称打开文件并以这种方式填充它们。您可以通过这种方式向页面添加任何内容。
background.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);
});
foreground.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;
}
所以假设我想注入一些代码以在 chrome 中的新 window 上创建一个按钮(window 本身是通过扩展程序的 popup.js 打开的).我该怎么做?
您可以在 foreground.js 中使用 documnet.body.appendChild() 之类的函数代替 console.log() ,然后用我给出的名称打开文件并以这种方式填充它们。您可以通过这种方式向页面添加任何内容。
background.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);
});
foreground.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;
}