-
-
Save devdave/c16deb02a8a44a19381f1d95f4d5da8e to your computer and use it in GitHub Desktop.
Message passing of Chrome Extension example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/***************************************************************** | |
* onMessage from the extension or tab (a content script) | |
*****************************************************************/ | |
chrome.runtime.onMessage.addListener( | |
function(request, sender, sendResponse) { | |
if (request.cmd == "any command") { | |
sendResponse({ result: "any response from background" }); | |
} else { | |
sendResponse({ result: "error", message: `Invalid 'cmd'` }); | |
} | |
// Note: Returning true is required here! | |
// ref: http://stackoverflow.com/questions/20077487/chrome-extension-message-passing-response-not-sent | |
return true; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "example", | |
"version": "0.0.1", | |
"description": "Chrome Extension's message passing example", | |
"permissions": [ | |
"<all_urls>", | |
"webRequest", | |
"webRequestBlocking", | |
"background", | |
"activeTab" | |
], | |
"background": { | |
"scripts": ["background.js"] | |
// "persistent": false | |
}, | |
"content_scripts": [{ | |
//"matches": ["http://*/*"], | |
// "css": ["mystyles.css"], | |
"js": [ | |
"extlib/js/jquery.min.js", | |
"content.js" | |
] | |
}], | |
"browser_action": { | |
"default_title": "hoge", | |
"default_icon": "icon.png", | |
"default_popup": "popup.html" | |
}, | |
"manifest_version": 2, | |
"content_security_policy": "script-src 'self' https://ajax.googleapis.com https://maxcdn.bootstrapcdn.com/; object-src 'self'" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// If you want to sendMessage from any popup or content script, | |
// use `chrome.runtime.sendMessage()`. | |
// Send message to background: | |
chrome.runtime.sendMessage(p, function(response) { | |
console.log(`message from background: ${JSON.stringify(response)}`); | |
}); | |
// If you want to sendMessage from tab of browser, | |
// use `chrome.tabs.sendMessage()`. | |
// Send message from active tab to background: | |
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) { | |
chrome.tabs.sendMessage(tabs[0].id, p, function(response) { | |
console.log(`message from background: ${JSON.stringify(response)}`); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment