From e73d405c5640afda27775285f19516a271b5cefb Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sat, 12 Aug 2017 11:08:43 +0900 Subject: [PATCH] message passing between background and content --- manifest.json | 5 ++++- src/background/index.js | 4 ++++ src/content/index.js | 9 +++++++++ src/index.js | 3 --- src/module.js | 18 ------------------ webpack.config.js | 3 ++- 6 files changed, 19 insertions(+), 23 deletions(-) create mode 100644 src/background/index.js create mode 100644 src/content/index.js delete mode 100644 src/index.js delete mode 100644 src/module.js diff --git a/manifest.json b/manifest.json index 61c815a..2621c5e 100644 --- a/manifest.json +++ b/manifest.json @@ -8,5 +8,8 @@ "matches": [ "http://*/*", "https://*/*" ], "js": [ "build/index.js" ] } - ] + ], + "background": { + "scripts": ["build/background.js"] + } } diff --git a/src/background/index.js b/src/background/index.js new file mode 100644 index 0000000..ee02f63 --- /dev/null +++ b/src/background/index.js @@ -0,0 +1,4 @@ +browser.runtime.onMessage.addListener((request, sender, sendResponse) => { + console.log("Pressed " + request.which); + sendResponse({ response: "Response from background script" }); +}); diff --git a/src/content/index.js b/src/content/index.js new file mode 100644 index 0000000..0fa0720 --- /dev/null +++ b/src/content/index.js @@ -0,0 +1,9 @@ +window.addEventListener("keypress", (e) => { + browser.runtime.sendMessage({ + which: e.which || e.keyCode, + }).then((msg) => { + console.log(`Message from the background script: ${msg.response}`); + }, (err) => { + console.log(`Error: ${err}`); + }); +}); diff --git a/src/index.js b/src/index.js deleted file mode 100644 index 8c9b627..0000000 --- a/src/index.js +++ /dev/null @@ -1,3 +0,0 @@ -import * as Module from './module'; - -Module.initialize() diff --git a/src/module.js b/src/module.js deleted file mode 100644 index e967a62..0000000 --- a/src/module.js +++ /dev/null @@ -1,18 +0,0 @@ -const initialize = () => { - let p = document.createElement("p"); - p.textContent = "Hello Vim Vixen"; - p.style.position = 'fixed'; - p.style.right = '0'; - p.style.bottom = '0'; - p.style.padding = '0rem .5rem'; - p.style.margin = '0'; - p.style.backgroundColor = 'lightgray'; - p.style.border = 'gray'; - p.style.boxShadow = '0 0 2px gray inset'; - p.style.borderRadius = '3px'; - p.style.fontFamily = 'monospace'; - - document.body.append(p) -} - -export { initialize }; diff --git a/webpack.config.js b/webpack.config.js index 4b3ed7a..bb1568d 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -5,7 +5,8 @@ const dist = path.resolve(__dirname, 'build'); module.exports = { entry: { - index: path.join(src, 'index.js') + index: path.join(src, 'content'), + background: path.join(src, 'background') }, output: {