I have a WebExtension with the following structure:
webextension [directory]
- background.js
- page1.html
- page1.js
- page2.html
- page2.js
The background.js listens for errors. If any, it instructs its callback function to update the tab with an HTML page, page2.js that contains a button.
The HTML script page2.js starts by sending a message to the background.js and background.js replies to page2.js. This part works fine.
Then, as you see in the code, page2.html contains a button if clicked, it will execute code in the callback function. Then, it will call refreshIndexPage
which should send a message to page1.js which is attached to page1.html.
The Problem: When I added the messaging APIs between page2.js and page1.js in the refreshIndexPage
, the message from page2 gets sent to the background.js. I do not want this to happen. As I will show in the output, I get: In background.js: received: undefined
Questions:
- How can you send a message from page2.js to page1.js without making background.js that also listens for a message from page2.js gets confused and receives the message that is not meant for it? How to specify that this message it is from page2.js to page1.js?
Here is the output after adding messages from page2.js to page1.js. Console output
inside refreshIndexPage
In background.js: received: undefined
inside handleRefreshResponse
- page1.html:
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h1>Pag1.html</h1>
<input type="button" id="page1-button" value="click"></input>
<script src="page1.js"></script>
</body>
</html>
- page1.js:
function displayAll() {
console.log("inside display all");
} //end displayAll
//should listen to messages from pag2.js
browser.runtime.onMessage.addListener(handleRefreshMessage);
function handleRefreshMessage(request, sender, sendResponse) {
console.log("In page1.js: message received" + request.refreshRequest);
sendResponse("response from page1.js to page2.js");
}
- background.js:
console.log("inside background");
//add a listener for the toolbar icon. If clicked, open page1.html
browser.browserAction.onClicked.addListener((tab) => {
// disable the active tab
var creating = browser.tabs.create({"url": "page1.html"});
creating.then((tab) => {
browser.browserAction.setIcon({tabId: tab.id, path: "icons/red-64.png"});
});//end creating.then
});//end addListener
var target = "<all_urls>";
function log(responseDetails) {
console.log("inside response details");
errorTab = responseDetails.tabId;
if(true) {
console.log("inside if");
browser.tabs.update(responseDetails.tabId,{url: "page2.html"});
//this message to wait request from page2.js
browser.runtime.onMessage.addListener(handleMessage);
} //end if
}//end log
function handleMessage(request, sender, sendResponse) {
console.log("In background.js: received: " + request.scriptRequest);
sendResponse({errorTab: errorTab});
}
var errorListening = browser.webRequest.onErrorOccurred.addListener(log, {
urls: [target],
types: ["main_frame"]
});
- page2.html:
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h1>Pag2.html</h1>
<input type="button" id="page2-button" value="click"></input>
<script src="page2.js"></script>
</body>
</html>
- page2.js:
/*self-calling function conatins sendMessage to background script*/
(function notifyBackgroundPage() {
var sending = browser.runtime.sendMessage({
scriptRequest: "From pag2.js. request for data"
});
sending.then(handleResponse);
})();
function handleResponse(message) {
console.log(`In page2.js: data from background is: ${message.errorTab}`);
} //handleResponse
function myFunction() {
refreshIndexPage();//end .then
}//end myFunction
//refreshIndexPage should send message to page1.js only when the button is clicked.
function refreshIndexPage() {
console.log("inside refreshIndexPage");
var sending = browser.runtime.sendMessage({
refreshRequest: "From page2.js"
});
sending.then(handleRefreshResponse);
}//end refreshIndex
function handleRefreshResponse() {
console.log("inside handleRefreshResponse");
}//end handleRefreshResponse
var page2Button = document.getElementById("page2-button");
page2Button.addEventListener('click', myFunction);
@[username]
) only works for users who have already interacted with the post (i.e. the author, an editor, has posted comments, and some other things). Thus, pinging me like you did here doesn't actually send me a message. Now, once I've posted this comment, pinging me here will send me a message. – Schoolteacher{
and}
) throughout the code that you place in a question. There aren't any requirements that you use one format over another, just be consistent, at least with all the code you have written within a single question. Doing so makes it significantly easier to read the code. If you desire, there are a variety of programs which will format JavaScript/HTML/CSS for you. – Schoolteacher