bon*_*LAP 7 javascript google-chrome google-chrome-extension
请原谅我的任何明显错误,因为我是 chrome 扩展程序的新手,但是 Chrome 消息传递 API 的这个错误已经在这里、这里和这里讨论过,常见的响应是“禁用现有的 Chrome 扩展程序,一个他们中的一个导致错误'。这是能做到的最好的吗?我们是否应该只是滚动并接受我们的扩展会与其他扩展发生冲突的事实?为侦听器回调函数返回 true 或返回 Promise 并使用sendResponse并不能解决我的问题。
目前,我只能chrome.storage.local通过禁用所有其他 chrome 扩展、删除扩展并重新加载解压缩的扩展来获取存储在(无错误)中的新值。有趣的是,该代码似乎只适用于 developer.chrome.com,它根本不适用于manifest.json.
我认为awaitandasync操作符在解决这个问题上有一定的意义,但我不确定如何正确实现它。
清单.json:
{
"manifest_version": 2,
"name": "my extension",
"version": "1.0",
"description": "its my extension",
"permissions": [
"declarativeContent",
"storage",
"activeTab"
],
"content_scripts": [
{
"matches": [
"*://developer.chrome.com/*",
"*://bbc.co.uk/*",
"*://theguardian.com/*",
"*://dailymail.co.uk/*"
],
"js": ["content.js"]
}
],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"content_security_policy": "script-src 'self' https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js; object-src 'self'",
"page_action": {
"default_popup": "popup.html"
},
"icons": {
"16": "images/icon16.png",
"32": "images/icon32.png",
"48": "images/icon48.png",
"128": "images/icon128.png"
}
}
Run Code Online (Sandbox Code Playgroud)
弹出窗口.html:
<!DOCTYPE html>
<html>
<head>
<title>my extension</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="popup.js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>my extension</h1>
<h2>Article: <span id="article-headline"></span></h2>
<button id="detect-article">Detect Article</button>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
popup.js:
$(document).ready(function() {
$("#detect-article").click(function() {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
chrome.tabs.sendMessage(tabs[0].id, {request: "Requesting headline"}, function(response) {
console.log("Requesting headline")
});
});
});
})
function getHeadline(changes) {
let changedValues = Object.keys(changes);
//console.log(changedValues);
for (var item of changedValues) {
console.log("new value: " + changes[item].newValue);
$("#article-headline").text(changes[item].newValue)
}
}
chrome.storage.onChanged.addListener(getHeadline);
Run Code Online (Sandbox Code Playgroud)
内容.js:
function handleRequest(message, sender, sendResponse) {
console.log("Request recieved");
let headlineList = document.getElementsByTagName("h1");
chrome.storage.local.set({headline: headlineList[0].innerText}, function() {
console.log("'" + headlineList[0].innerText + "' stored in local storage");
});
return true;
}
chrome.runtime.onMessage.addListener(handleRequest);
Run Code Online (Sandbox Code Playgroud)
背景.js:
chrome.runtime.onInstalled.addListener(function() {
chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
chrome.declarativeContent.onPageChanged.addRules([{
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { hostContains: 'developer.chrome.com' },
}),
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { hostContains: 'bbc.co.uk' },
}),
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { hostContains: 'theguardian.com' },
}),
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { hostContains: 'dailymail.co.uk' },
}),
],
actions: [new chrome.declarativeContent.ShowPageAction()]
}]);
});
});
Run Code Online (Sandbox Code Playgroud)
非常感谢您花时间查看/重新查看此问题,与上述“禁用现有扩展”相关的解决方案不是我正在寻找的。
wOx*_*xOm 24
当您为 sendMessage 指定回调时,您是在告诉 API您需要一个响应,因此当您的内容脚本没有使用 sendResponse 响应时,API 认为发生了可怕的事情并报告它!
提醒:编辑内容脚本时,请确保重新加载
chrome://extensions页面上的扩展程序和应具有此内容脚本的选项卡。
你不需要任何回应:
移除 sendMessage 中的回调
chrome.tabs.sendMessage(tabs[0].id, {request: "Requesting headline"});
Run Code Online (Sandbox Code Playgroud)
删除return true- 它当前所做的只是告诉 API 无限期地保持消息传递端口打开,您永远不会使用它,因此它只是内存泄漏源。
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
// do something
// don't call sendResponse, don't return true
});
Run Code Online (Sandbox Code Playgroud)
你需要一个回应:
替换return true为sendResponse
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
sendResponse('whatever');
});
Run Code Online (Sandbox Code Playgroud)
您需要异步运行的代码(例如chromeAPI 回调)的响应:
保持 return true
调用sendResponse(someImportantData)回调函数内
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
chrome.storage.local.set({foo: 'bar'}, () => {
sendResponse('whatever');
});
return true;
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6157 次 |
| 最近记录: |