gpt4 book ai didi

javascript - Chrome扩展中JS/HTML之间的通信

转载 作者:行者123 更新时间:2023-12-03 06:41:32 25 4
gpt4 key购买 nike

我正在尝试在没有任何 JS 知识的情况下制作我的第一个 Chrome 扩展,但在做这件事时遇到了一些麻烦。

扩展程序有什么作用?

这是一个页面操作扩展,用于生成字符串并将其复制到剪贴板。该字符串包含 DOM 中的某些元素属性。

范围

仅适用于两个页面(以下域为示例):

  1. https://xxx.abc.com/CFM/Messages/CFMEWFA/ *
  2. https://xxx.abc.com/CFM/Messages/FraudPrevention/ *

扩展的元素

该扩展程序有一个 popup.html,其中包含三个可点击的选项,由用户自行选择:

  • 没有回应
  • 无效
  • 有效

该字符串的格式取决于用户在弹出窗口中的选择以及选项卡 URL 是否包含“CFMEWFA”或“FraudPrevention”。

popup.html

<!doctype html>
<html>

<body>
<script src="popup.js"></script>
<ul id="MENU">
<li id="MENUnoResponse"><a href="#">No reponse</a>
</li>
<li id="MENUinValid"><a href="#">Invalid</a>
</li>
<li id="MENUvalid"><a href="#">Valid</a>
</li>
</ul>
</body>

</html>

popup.js 应该监听 popup.html 中的点击,使用多项目点击处理程序,然后在点击事件时向 background.js 发送消息。该消息应包含与 popup.html 中的 li id 相对应的参数。

popup.js

var theParentMenu = document.querySelector("#MENU");
theParentMenu.addEventListener("click", userHasClicked, false);

function userHasClicked(e) {
if (e.target !== e.currentTarget) {
var clickedItem = e.target.id;
chrome.runtime.sendMessage({
directive: e.target.id
}, function(response) {
this.close();
});
};
e.stopPropagation();
}

background.js 控制扩展程序图标的显示位置。在执行 content.js 之前,它还会监听来自 popup.js 的消息(包含由用户从 popup.html 中选择确定的参数),该脚本在 tab.url 中运行,从 DOM 获取属性并生成字符串。由于其他文件中之前存在 Unresolved 问题,我尚未开始构建 content.js。

背景.js

//Displays the page action extension only on specific pages
function checkForValidUrl(tabId, changeInfo, tab) {

if (tab.url.indexOf("https://xxx.abc.com/CFM/Messages/FraudPrevention/") == 0)
{
chrome.pageAction.show(tabId);
}
else if (tab.url.indexOf("https://xxx.abc.com/CFM/Messages/CFMEWFA/") == 0)
{
chrome.pageAction.show(tabId);
}
};

chrome.tabs.onUpdated.addListener(checkForValidUrl)


chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
switch (request.directive) {
case "MENUnoReponse":
// execute the content script
chrome.tabs.executeScript(null, { // defaults to the current tab
//file: "contentscript.js", // script to inject into page and run in sandbox
//allFrames: true // This injects script into iframes in the page.
});
sendResponse({}); // sending back empty response to sender
case "MENUinValid":
// execute the content script
chrome.tabs.executeScript(null, { // defaults to the current tab
//file: "contentscript.js", // script to inject into page and run in sandbox
//allFrames: true // This injects script into iframes in the page.
});
sendResponse({}); // sending back empty response to sender
case "MENUvalid":
// execute the content script
chrome.tabs.executeScript(null, { // defaults to the current tab
//file: "contentscript.js", // script to inject into page and run in sandbox
//allFrames: true // This injects script into iframes in the page.
});
sendResponse({}); // sending back empty response to sender

break;
default:
// helps debug when request directive doesn't match
alert("Unmatched request of '" + request + "' from script to background.js from " + sender);
}
}
);

ma​​nifest.json

{
"manifest_version": 2,

"name": "EW logger",
"description": "This extension creates logs for early warning and fraud prevention cases",
"version": "1.0",

"page_action": {
"default_title": "EW",
"default_icon": "icon.png",
"default_popup": "popup.html"
},

"background": {
"scripts": ["background.js"]
},

"permissions": [
"tabs",
"clipboardWrite",
"https://xxx.abc.com/*"
]

}

什么有效:

  • 扩展程序图标按其应有的方式显示。

我的问题:

  • popup.html 中的选项不起作用。当我点击时 Popup.js 不执行任何操作。

您对如何正确“监听”popup.html 中的点击,然后将包含参数的消息发送到background.js 有什么建议吗?

最佳答案

您的脚本在加载正文之前运行,因此找不到该元素。您可以通过将脚本标签移动到正文的底部来解决此问题。或者,使用<script src="popup.js" defer></script>延迟执行直到 dom 加载完毕。

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script#attr-defer

此外,您应该使用 console.log(message) 和 Chrome Devtools 控制台来调试和检查错误。

https://developer.mozilla.org/en-US/docs/Web/API/Console/log https://developers.google.com/web/tools/chrome-devtools/

关于javascript - Chrome扩展中JS/HTML之间的通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37935100/

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