message passing between background and content

This commit is contained in:
Shin'ya Ueoka 2017-08-12 11:08:43 +09:00
parent d169661e03
commit e73d405c56
6 changed files with 19 additions and 23 deletions

4
src/background/index.js Normal file
View file

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

9
src/content/index.js Normal file
View file

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

View file

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

View file

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