message passing between background and content

jh-changes
Shin'ya Ueoka 7 years ago
parent d169661e03
commit e73d405c56
  1. 5
      manifest.json
  2. 4
      src/background/index.js
  3. 9
      src/content/index.js
  4. 3
      src/index.js
  5. 18
      src/module.js
  6. 3
      webpack.config.js

@ -8,5 +8,8 @@
"matches": [ "http://*/*", "https://*/*" ], "matches": [ "http://*/*", "https://*/*" ],
"js": [ "build/index.js" ] "js": [ "build/index.js" ]
} }
] ],
"background": {
"scripts": ["build/background.js"]
}
} }

@ -0,0 +1,4 @@
browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
console.log("Pressed " + request.which);
sendResponse({ response: "Response from background script" });
});

@ -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}`);
});
});

@ -1,3 +0,0 @@
import * as Module from './module';
Module.initialize()

@ -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 };

@ -5,7 +5,8 @@ const dist = path.resolve(__dirname, 'build');
module.exports = { module.exports = {
entry: { entry: {
index: path.join(src, 'index.js') index: path.join(src, 'content'),
background: path.join(src, 'background')
}, },
output: { output: {