From 67cd686e19dadbcff60add8f2349207f84fbd452 Mon Sep 17 00:00:00 2001 From: Hennerz Date: Wed, 12 Oct 2022 13:46:01 +0100 Subject: [PATCH 1/3] not working --- src/pages/Background/index.js | 14 ++++++++++++++ src/pages/Content/index.js | 2 ++ src/pages/Popup/Popup.jsx | 18 ++++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/src/pages/Background/index.js b/src/pages/Background/index.js index 8943b846f..24a859846 100644 --- a/src/pages/Background/index.js +++ b/src/pages/Background/index.js @@ -1,2 +1,16 @@ console.log('This is the background page.'); console.log('Put the background scripts here.'); + +const database = []; + +chrome.runtime.onMessage.addListener( + function (request, sender, sendResponse) { + console.log(sender.tab ? + "from a content script:" + sender.tab.url : + "from the extension"); + + database.push(request.greeting); + if (request.greeting === "hello") + sendResponse({ farewell: "goodbye" }); + } +); diff --git a/src/pages/Content/index.js b/src/pages/Content/index.js index d9e44cee1..558b648de 100644 --- a/src/pages/Content/index.js +++ b/src/pages/Content/index.js @@ -3,4 +3,6 @@ import { printLine } from './modules/print'; console.log('Content script works!'); console.log('Must reload extension for modifications to take effect.'); + + printLine("Using the 'printLine' function from the Print Module"); diff --git a/src/pages/Popup/Popup.jsx b/src/pages/Popup/Popup.jsx index df1684b13..bf33f3dd7 100644 --- a/src/pages/Popup/Popup.jsx +++ b/src/pages/Popup/Popup.jsx @@ -4,6 +4,21 @@ import './Popup.css'; import { Text } from '@chakra-ui/react' const Popup = () => { + const [name, setName] = React.useState(''); + const handleClick = () => { + console.log('handleClick', name); + // chrome.runtime.sendMessage({ name }, function (response) { + // console.log(response.farewell); + // }); + chrome.runtime.sendMessage({ + type: "notification", options: { + type: "basic", + iconUrl: chrome.extension.getURL("icon128.png"), + title: "Test", + message: "Test" + } + }); + } return (
@@ -14,6 +29,9 @@ const Popup = () => { PLANES + + setName(e.target.value)} /> + Date: Wed, 12 Oct 2022 20:17:13 +0100 Subject: [PATCH 2/3] fix: set up communication between background and popup --- src/pages/Background/index.js | 17 ++++++----------- src/pages/Popup/Popup.jsx | 17 ++++++----------- 2 files changed, 12 insertions(+), 22 deletions(-) diff --git a/src/pages/Background/index.js b/src/pages/Background/index.js index 24a859846..b57ba9949 100644 --- a/src/pages/Background/index.js +++ b/src/pages/Background/index.js @@ -1,16 +1,11 @@ console.log('This is the background page.'); console.log('Put the background scripts here.'); -const database = []; -chrome.runtime.onMessage.addListener( - function (request, sender, sendResponse) { - console.log(sender.tab ? - "from a content script:" + sender.tab.url : - "from the extension"); +chrome.runtime.onMessage.addListener(messageReceived); - database.push(request.greeting); - if (request.greeting === "hello") - sendResponse({ farewell: "goodbye" }); - } -); +function messageReceived(msg, sender, sendResponse) { + console.log('inside the background paghe', msg, sender, sendResponse) + return sendResponse({ farewell: "goodbye" }); + +} \ No newline at end of file diff --git a/src/pages/Popup/Popup.jsx b/src/pages/Popup/Popup.jsx index bf33f3dd7..bb7359827 100644 --- a/src/pages/Popup/Popup.jsx +++ b/src/pages/Popup/Popup.jsx @@ -5,19 +5,14 @@ import { Text } from '@chakra-ui/react' const Popup = () => { const [name, setName] = React.useState(''); + const handleClick = () => { - console.log('handleClick', name); - // chrome.runtime.sendMessage({ name }, function (response) { - // console.log(response.farewell); - // }); - chrome.runtime.sendMessage({ - type: "notification", options: { - type: "basic", - iconUrl: chrome.extension.getURL("icon128.png"), - title: "Test", - message: "Test" - } + console.log('handleClick function!',); + + chrome.runtime.sendMessage('{ greeting: name }', function (response) { + console.log('response in popup click handler', response); }); + } return (
From e2371b56fc3a3c8f437d830bda907aec6e2656ff Mon Sep 17 00:00:00 2001 From: Seamus1989 Date: Thu, 13 Oct 2022 12:29:00 +0100 Subject: [PATCH 3/3] fix: lets make these things chat --- src/pages/Content/index.js | 8 ++++++++ src/pages/Popup/Popup.jsx | 10 +++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/pages/Content/index.js b/src/pages/Content/index.js index 558b648de..525becbd7 100644 --- a/src/pages/Content/index.js +++ b/src/pages/Content/index.js @@ -6,3 +6,11 @@ console.log('Must reload extension for modifications to take effect.'); printLine("Using the 'printLine' function from the Print Module"); + +chrome.runtime.onMessage.addListener(messageReceived); + +function messageReceived(msg, sender, sendResponse) { + console.log('inside the CONTENT paghe', msg, sender, sendResponse) + return sendResponse({ farewell: "goodbye" }); +} + diff --git a/src/pages/Popup/Popup.jsx b/src/pages/Popup/Popup.jsx index bb7359827..9e500b688 100644 --- a/src/pages/Popup/Popup.jsx +++ b/src/pages/Popup/Popup.jsx @@ -9,10 +9,18 @@ const Popup = () => { const handleClick = () => { console.log('handleClick function!',); - chrome.runtime.sendMessage('{ greeting: name }', function (response) { + /*chrome.runtime.sendMessage('{ greeting: name }', function (response) { console.log('response in popup click handler', response); + });*/ + + + chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) { + chrome.tabs.sendMessage(tabs[0].id, { type: name }, function (response) { + alert(response) + }); }); + } return (