Chrome extension: How to sendMessage form background to background?

时光怂恿深爱的人放手 提交于 2019-12-06 09:24:13

Messages dispatched by a page are not received by the same page.

If you want to be able to re-use the onMessage listener, put it in a separate function. For example:

function alwaysDoSomething() {
    console.log('Done something!');
}
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
    alwaysDoSomething();
});
chrome.contextMenus.onClicked.addListener(function(info, tab) {
    alwaysDoSomething();
});

There is an undocumented method that can be used to manually trigger the events. It is undocumented, so use it at your own risk!

chrome.contextMenus.onClicked.addListener(function(info, tab) {
    var message = 'whatever';
    var sender = {tab: null, id: chrome.runtime.id};
    var sendResponse = function() {};
    chrome.runtime.onMessage.dispatch(message, sender, sendResponse);
});

I think you should use chrome.extension.onRequestExternal or chrome.extension.onConnectExternal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!