亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

chrome.tabs.query 未將消息傳遞到內容.js

chrome.tabs.query 未將消息傳遞到內容.js

HUX布斯 2022-09-02 17:19:18
當我開始學習chrome擴展時,我被困在一個基本程序中。我的程序是構建一個擴展,該擴展具有更改網頁顏色的按鈕。更改顏色應該是操作驅動的,這就是為什么我沒有使用默認css在頁面加載時更改顏色的原因。以下是我的文件。我遇到的問題是后臺的chrome.tabs.query.js沒有返回tabs數組,我得到“ID未引用錯誤”任何幫助都是值得贊賞的。我已經在谷歌和StackOverflow上搜索了n篇文章,但沒有什么可以幫助我Files: manifest.json    {"manifest_version": 2,"name": "Hello world","description": "Demo extension", "version": "1.0", "icons":{"16": "images/dog.png", "48": "images/dog.png","128": "images/dog.png"},"background": {        "persistent": false,        "scripts": ["background.js"]    },"browser_action":{"default_icon": "images/dog.png","default_popup": "popup.html"},"content_scripts": [{    "js": ["content.js"],    "matches": ["<all_urls>"]  }],"permissions": ["tabs", "http://*/*","activeTab"]}背景.jschrome.runtime.onMessage.addListener(    function(message, sender, sendResponse) {        switch(message.type) {            case "setcolor":                chrome.tabs.query({active: true, currentWindow: true}, function(tab){                chrome.tabs.sendMessage(tab[0].id,{type: "setcolor",color: message.color})                });                break;            default:                console.error("Unrecognised message: ", message);        }彈出窗口.html<!DOCTYPE html><html><head></head><body><p id="demo">Click below button to change color of page</p><button type="button" id = "red"> RED </button><script src = "myscript.js"></script></body></html> myscript.js(或彈出窗口.js)function color(colorval) {    chrome.runtime.sendMessage({type: "setcolor",color: colorval})}document.getElementById('red').addEventListener('click', color('red'));內容.jschrome.runtime.onMessage.addListener(    function(message, sender, sendResponse) {        document.body.style.backgroundColor = message.color;        });
查看完整描述

2 回答

?
慕蓋茨4494581

TA貢獻1850條經驗 獲得超11個贊

我以錯誤的方式使用以下函數。

舊代碼:

document.getElementById('red').addEventListener('click', color('red'));

新代碼:

document.getElementById('red').addEventListener('click', () => color('red'));

document.getElementById('red').addEventListener('click', fucntion() {color('red')});



查看完整回答
反對 回復 2022-09-02
?
四季花海

TA貢獻1811條經驗 獲得超5個贊

嘗試從回調中刪除作為第一個參數。Tab


chrome.tabs.query({active: true, currentWindow: true}, function(tabs){

   // your message logic

});


查看完整回答
反對 回復 2022-09-02
  • 2 回答
  • 0 關注
  • 248 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號