Guest User

main.js

a guest
Feb 23rd, 2018
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const context = {
  2.     port: null,
  3. }
  4.  
  5. const messages = [
  6.     "ping",
  7.     "pong",
  8.     "foobar",
  9.     "Ф",
  10. ];
  11. let index = 0;
  12.  
  13. function initNativePort() {
  14.     const port = context.port = browser.runtime.connectNative('webextdev');
  15.     port.onMessage.addListener((aResponse) => {
  16.         console.log('Native message', aResponse);
  17.     });
  18. }
  19.  
  20. browser.browserAction.onClicked.addListener(() => {
  21.     if (!context.port) {
  22.         initNativePort();
  23.     }
  24.  
  25.     console.log('Sending native message');
  26.     const cMessage = messages[index % messages.length];
  27.     ++index;
  28.     context.port.postMessage(cMessage);
  29. });
Advertisement
Add Comment
Please, Sign In to add comment