gpt4 book ai didi

javascript - 如何让 chrome 扩展按钮与注入(inject)的 js 通信?

转载 作者:行者123 更新时间:2023-11-30 16:01:43 26 4
gpt4 key购买 nike

我有一个 chrome 扩展程序,可以将 js 文件注入(inject)网页。在浏览器中单击时还会打开一个弹出窗口。在弹出窗口中有一个按钮应该更改 js 文件中的变量。但是注入(inject)的文件通常无法与扩展的其余部分通信。我该如何更改?

我对 chrome 扩展还是有点陌生​​,谢谢你的帮助。

编辑:这是我目前所拥有的

list 文件:

     {
"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:?Subject=Minds%20Support" target="_top">Contact us</a></font>
</div>
</div>
</body>
</html>

注入(inject)js.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,我可以将它们提供给您,但我不明白您为什么需要它们。

最佳答案

要了解的重要一点是,注入(inject)的脚本与网页交互,但与弹出窗口(或背景)中发生的事情是分开的。因此,虽然您将 injected.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;
}
}

一些最后的想法:

  • 由于您的扩展针对特定页面,因此您应该使用页面操作而不是浏览器操作。差异过去更为重要(您的扩展名根本不会出现)。如今,除非您在正确的页面上,否则您将无法打开弹出窗口。
  • 如果您的 injected.css 确实是空的,那么您可以简单地从 manifest.conent_scripts 条目中删除 css 键。
  • 为了让 getElementById 在 popup.js 中工作,您需要在元素出现后运行脚本。您可以将所有内容放入onload 调用中,但将script 标记移动到body 之前可能更容易关闭。

关于javascript - 如何让 chrome 扩展按钮与注入(inject)的 js 通信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37622345/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com