This is the flow I'm trying to accomplish:
User clicks button on a regular webpage -> JSON message is sent to the extension -> Extension opens tab.
I understand that content scripts are needed to send the message to the extension:
contentscript.js
chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
console.log(response.farewell);
});
src: Simple Messaging
some arbitrary "listener"
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
if (request.greeting == "hello")
sendResponse({farewell: "goodbye"});
});
src: Simple Messaging
Now, I'm confused about two things:
- How will
contentscript.js
talk to JSON variables loaded in the webpage? - Which file listens to the messages?