runtime.onMessage没有收到消息

时间:2017-09-01 22:26:26

标签: jquery

我正在尝试构建我的第一个chrome扩展,并且很难从popup.js发送消息到work.js.任何帮助都非常感谢。

Popup.js脚本

var but = document.getElementById('ok_btn');

if (but )
{
    but.addEventListener("click", saveChanges , false);
}
function saveChanges() {
    var username = document.getElementById('username').value;
    alert(username);
    chrome.runtime.sendMessage({ website: username, 
    message:"Username_Received"});

    chrome.tabs.executeScript({file: 'work.js'});
    window.close();
}

Work.js

chrome.runtime.onMessage.addListener(function(request,sender,sendResponse){
    console.log('reached');
    alert('inside message request');
    if(request.website == "Username_Received"){
       alert(request.website);
    }
});

0 个答案:

没有答案