簡體   English   中英

Chrome擴展程序:sendMessage不起作用

[英]Chrome extension: sendMessage doesn't work

我已經閱讀了谷歌關於“消息傳遞”的文檔幾次,並且可能已經查看過10個其他問題同樣的問題,並且已經嘗試過安靜一些大多數“解決方案”的變體以及我在下面的內容......這是黑魔法吧? 無論哪種方式,它都在這里。

清單文件:

{
    "manifest_version" : 2,
    "name" : "Message Test",
    "version" : "1.0",

    "browser_action": { 
        "default_popup": "popup.html"
    },

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

    "content_scripts": [
        {
        "matches" : ["<all_urls>"],
        "js": ["message-test.js"]
        }
    ]    
}

我知道擴展不是假設使用內聯JS,但是我將其保留在原來的問題可以保留原來的問題因為我仍然無法從后台頁面發送消息,當我切換時從彈出窗口到后台,我從manifest.json中刪除了相應的行

popup.html文件:

<html>
  <head>
    <script>
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
      chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello", theMessage: "Why isn\'t this working?"}, function(response) {
        console.log(response.farewell);
      });
    });
    </script>
  </head>
<body>
</body>
</html>

要么

background.js文件:

chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
  chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello", theMessage: "Why isn\'t this working?"}, function(response) {
    console.log(response.farewell);
  });
});

message-test.js文件:

var Mymessage;
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
    if (message.greeting == "hello"){
        Mymessage = message.theMessage;
        alert(Mymessage);
    }
    else{
        sendResponse({});
    }
});

我得到一個未定義的警報......

我也試圖在彈出一個按鈕並在指定的URL上有一個窗口后執行此操作,但這是后來的問題。 按鈕和窗口的其他文件可以在這里找到,除了background.js內容包含在addEventListener中(“點擊”....: http//pastebin.com/KhqxLx5yhttp://pastebin.com/ JaGcp6tj

您的代碼中存在幾個問題。

Chrome不允許擴展中的內聯腳本。 您必須將popup.html划分為腳本+ HTML:

// popup.html
<html>
<body>
<script type="text/javascript" src="popup.js"></script>
</body>
</html>

// popup.js
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
    var tab = tabs[0];  // do not forget to declare "tab" variable
    chrome.tabs.sendMessage(tab.id, {
        greeting: "Can you hear me?"
    }, function(response){});
});

仔細閱讀這個答案可能對您有所幫助。 每個點都適用於您的問題。

問題在於您的主腳本執行時以及內容腳本何時出現。 您需要確保在向其發送消息之前監聽內容腳本,在最壞的情況下以編程方式注入它。

為了讓它在彈出窗口中工作,請按照KAdot的回答

以下是使用后台腳本的解決方案:

的manifest.json

{
    "manifest_version" : 2,
    "name" : "Message Test",
    "version" : "1.0",

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

    "content_scripts": [
        {
        "matches" : ["<all_urls>"],
        "js": ["message-test.js"]
        }
    ]
}

消息test.js

var port = chrome.runtime.connect();
port.onMessage.addListener(function(message, sender, sendResponse) {
    if (message.greeting == "Can you hear me?"){
        alert("Test");
    }
    else{
        sendResponse({});
    }
});

popup.js

chrome.runtime.onConnect.addListener( function ( port ) {
port.postMessage({
        greeting: "Can you hear me?"
    });
});

一些解釋:首先我們從內容腳本連接到我們的后台腳本,然后后台腳本將消息發送到內容腳本。

更新

我根據@Xan評論改進了答案。 但是這個想法是一樣的,首先你要知道你的后台腳本是否存在內容腳本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM