Skip to content

Instantly share code, notes, and snippets.

@TanisukeGoro
Created August 7, 2019 10:18
Show Gist options
  • Save TanisukeGoro/d9373483458410a58ee512f3a312a85d to your computer and use it in GitHub Desktop.
Save TanisukeGoro/d9373483458410a58ee512f3a312a85d to your computer and use it in GitHub Desktop.
Chrome拡張機能デモ用
// // メッセージの受信
chrome.runtime.onMessage.addListener(
function(request, sender, response) {
console.log(sender.tab.id);
response({ farewell: "goodbye" });
// return 0;
});
// メッセージの送信
const sendContentScript = greet => {
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, { greeting: greet }, function(response) {
// console.log(response);
});
});
}
// メッセージの受信①
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.greeting === 'greet_1') {
sendResponse({ farewell: "res_1" });
};
return true;
});
// メッセージの受信②
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.greeting === 'greet_2') {
sendResponse({ farewell: "res_2" });
};
return true;
});
// メッセージの受信③
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.greeting === 'greet_3') {
sendResponse({ farewell: "res_3" });
};
return true;
});
// // メッセージの送信
chrome.runtime.sendMessage({ greeting: "message_content_1" }, function(response) {
console.log(`from Popup pagge msg : ${response.farewell}`);
});
{
"name": "Message Passing TEST",
"description": "Message Passing TEST",
"version": "1.0",
"manifest_version": 2,
"browser_action": {
"default_popup": "popup.html"
},
"background": {
"scripts": [
"background.js"
],
"persistent": false
},
"permissions": [],
"content_scripts": [{
"matches": [
"http://*/*",
"https://*/*"
],
"js": [
"content_script.js",
"content_script2.js"
]
}]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment