我有一个chrome扩展程序,可以将js文件注入网页。并且在浏览器中单击时也会打开一个弹出窗口。在弹出窗口中有一个按钮,可以更改js文件中的变量。但是注入的文件通常无法与扩展的其余部分进行通信。我该如何改变呢?
我对Chrome扩展程序还有点新意,感谢您的帮助。
编辑:这是我到目前为止所拥有的
清单文件:
{
"manifest_version": 2,
"name": "Minds Color Extension",
"version": "1",
"content_scripts": [
{
"matches": ["https://www.minds.com/*"],
"js": ["injectedjs.js"],
"css" : ["injectedcss.css"]
}
],
"browser_action": {
"default_icon": {
"19": "icon.png",
"38": "icon.png"
},
"default_title": "Minds Notification page",
"default_popup": "popup.html"
}
}
Popup.html
<!--
<!doctype html>
This page is shown when the extension button is clicked, because the
"browser_action" field in manifest.json contains the "default_popup" key with
value "popup.html".
-->
<html>
<head>
<title>Minds Theme Extension</title>
<style>
html, body {
margin:0;
padding:10;
width: 250px;
}
#blue {
background-color: #337dff;
}
#orange {
background-color: #ff5733;
}
#dark {
background-color: #4a505b;
}
button {
border: none;
color: white;
padding: 10px 27px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
}
#footer {
position: fixed;
bottom: 0;
}
</style>
<!--
- JavaScript and HTML must be in separate files: see our Content Security
- Policy documentation[1] for details and explanation.
-
- [1]: https://developer.chrome.com/extensions/contentSecurityPolicy
-->
<script src="injectedjs.js"></script>
</head>
<body>
<div id="status">
<h2> Pick a theme: </h2>
<a href="blue.html"><button type="button" id="blue" onclick="activeBlue(); run();">Blue</button></a>
<a href="orange.html"><button type="button" id="orange" onclick="activeOrange(); run();">Orange</button></a>
<a href="dark.html"><button type="button" id="dark" onclick="activeDark(); run();">Dark</button></a>
<br />
<div id="footer">
<font size="1"> <a href="mailto:one.snapp8@gmail.com?Subject=Minds%20Support" target="_top">Contact us</a></font>
</div>
</div>
</body>
</html>
Injectedjs.js
// Var block
var blue = 0;
var orange = 0;
var dark = 0;
// Activation block
function activeBlue() {
blue = 1;
}
function activeOrange() {
orange = 1;
}
function activeDark() {
dark = 1;
}
// Resets var block
function reset() {
blue = 0;
orange = 0;
dark = 0;
}
//Changes Minds css (Runs theme)
function run() {
if (blue == 1) {
// Runs code that will change minds to blue.
document.getElementById("mdl-color--white").className = "mdl-color--blue";
}
if (orange == 1) {
// Runs code that will change minds to orange
document.getElementById("mdl-color--white").className = "mdl-color--orange";
}
if (dark == 1) {
// Runs code that will change minds to dark
document.getElementById("mdl-color--white").className = "mdl-color--grey";
}
}
Injectedcss.css中没有任何内容。如果你需要像dark.html这样的其他文件,我可以把它们给你,但我不明白为什么你需要它们。
答案 0 :(得分:2)
要理解的重要一点是注入的脚本与网页交互,但与弹出窗口(或您的背景)中发生的事件是分开的。因此,虽然您已将inject.js列为内容脚本并由popup.html加载,但实际上它们最终会成为两个单独的文件。
所以第一步是创建一个popup.js。它需要附加点击监听器(“onclick”计为不分离javascript和html)并使用message passing告诉内容脚本该做什么。 (我也整合了你的逻辑。)
["blue","orange","grey"].forEach(addListenerFor);
function addListenerFor(color) {
document.getElementById(color)
.addEventListener(click,turnPage.bind(undefined,color);
}
function turnPage(color) {
chrome.tabs.query({"active": true, "currentWindow": true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {"newClass": "mdl-color--"+color});
});
}
在内容脚本中,我们需要侦听消息并采取适当的措施:
chrome.runtime.onMessage.addListener(changeClass);
function changeClass(message) {
reset();
if ( message.newClass ) {
document.getElementById("mdl-color--white").className = message.newClass;
}
}
最后的一些想法:
css
条目中删除manifest.conent_scripts
密钥。getElementById
能够在popup.js中运行,您需要在元素存在后运行脚本。您可以将所有内容都放入onload
来电,但将script
代码移至body
关闭之前可能更容易。