From dd18c91b214c6e8b0c27c05ad11099f3b668cd8c Mon Sep 17 00:00:00 2001 From: abhijithvijayan <34790378+abhijithvijayan@users.noreply.github.com> Date: Fri, 25 Oct 2019 16:29:59 +0530 Subject: [PATCH] send sample message on popup click to background.js --- src/scripts/background.js | 15 ++++++++++++++- src/scripts/popup.js | 19 ++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/scripts/background.js b/src/scripts/background.js index b8bb883..3993747 100644 --- a/src/scripts/background.js +++ b/src/scripts/background.js @@ -1 +1,14 @@ -console.log('Hello World from background main file!'); +import browser from 'webextension-polyfill'; + +browser.runtime.onInstalled.addListener(() => { + // eslint-disable-next-line no-console + console.log('onInstalled....'); +}); + +browser.runtime.onMessage.addListener((request, sender, sendResponse) => { + // Do something with the message! + alert(request.url); + + // And respond back to the sender. + return Promise.resolve('got your message, thanks!'); +}); diff --git a/src/scripts/popup.js b/src/scripts/popup.js index 7f6c939..3c43dfa 100644 --- a/src/scripts/popup.js +++ b/src/scripts/popup.js @@ -1 +1,18 @@ -console.log('Hello World from popup main file!'); +import browser from 'webextension-polyfill'; + +document.addEventListener('DOMContentLoaded', async () => { + const tabs = await browser.tabs.query({ + active: true, + lastFocusedWindow: true, + }); + + const url = tabs.length && tabs[0].url; + + const response = await browser.runtime.sendMessage({ + msg: 'hello', + url, + }); + + // eslint-disable-next-line no-console + console.log(response); +});