What I am trying to do is:
when I click a button in popup.html, new page will be open in the case that it is not already open.
I have tried:
(popup.js)
if ( ( win in my_namespace.tabs ) && !my_namespace.tabs[win]
)
chrome.tabs.create(
{"url": url},
function(tab) {
if (browser.extension.lastError ) console.log(browser.extension.lastError);
my_namespace.tabs[win] = tab.id;
}
);
I have popup.html which opens when I click on button and from this popup the new page is opened. The popup.html contains
script src="…/background.js"/script
script src=“popup.js”/script
background.js contains my_namespaces, which should save the window it. But there is a problem, because the tab.id (saved) does not stay in the memory. It looks like the namespace is reinitiated when I click the button again.
Edit:
This script works until I have the popup open. When it is closed, and then opened again, it will reinitiate the namespace and so the value tab id is lost.
Any idea how to solve this?
Edit 2:
Last thing what I tried is to send message to extension to ask for the variable.
browser.runtime.sendMessage(
"open", win,
function(response) {
var p = 1;
console.log("response:");
console.log(response);
}
)
and added listener to background.js
chrome.runtime.onMessage.addListener(
function(m, sender
){
var c = 0;
console.log(m);
}
)
But I did not see any output or response. Added a breakpoint at the line with var c = 0;
but the program did not break here, maybe an exception “Object” happened. So I have no idea why I cannot send the message.
Edit 4:
I managed to send the message from popup script to extension (background.js)
browser.runtime.sendMessage(“open”, win)
I can recieve the message, but now it is not clear to me how should I send the response containing the variable in the response.
chrome.runtime.onMessage.addListener(
function(m, sender){
var c = 0;
switch (m){
case "open":
console.log("Do some action, print message");
console.log(m);
console.log(sender);
break;
}
}
)
Edit 5:
I have created onMessage listener in background.js:
chrome.runtime.onMessage.addListener(
function(package, sender
){
var c = 0;
switch (package.message){
case "open":
if (package.subject == "tab")
{
win = mynamespace.tabs[package.modul];
result = win ? false : true;
chrome.tabs.sendMessage(
sender.id,
{ message:"tab-response",
subject: {id: sender.id },
modul: package.modul,
result: result,
info: "response to tab exists message"
}
)
}
break;
}
}
)
I can send a testing query:
browser.runtime.sendMessage(
{ message:"open",
subject: "tab",
modul: win,
sender:{id:0}
}
);
This asks of the addon if there is tab id saved for the module which tab I want to create. If it is saved, it should respond with negative result.
Now I could create another listener for the response, but it is not clear to me how to make these things do what I expect.
The query should look like this:
chrome.tabs.query(
{ active: true, currentWindow: true},
function(tabs) {
browser.runtime.sendMessage(
{ message:"open",
subject: "tab",
modul: win,
sender:{id:tabs[0].id}
}
)
}
);
But what should be in the callback? Just to add the listener and into the listener add function containing chrome.tabs.create?
But this is second time when I need to create runtime.onMessage listener and only one listener can be created, right?
So the tab creation proccess should be handled from background.js, not from popup.js if I understand it. It’s quite complicated
Edit 6:
I think I have almost find how to do it. So I will not send response back but I will create the tab directly from addon as a reaction for the message sent from popup window.
This seems to me as best approach and not like in that poor example where I copied the popup code from.
The current problem however is that the onRemove event is not fired when I close the tab (which I created after the message has been sent).
chrome.runtime.onMessage.addListener(
function(package, sender
){
switch (package.message){
case "open":
if (package.subject == "tab")
{
win = myNamespace.tabs[package.modul];
result = win ? false : true;
if (result)
chrome.tabs.create(
{"url": package.url },
function(tab) {
if (chrome.extension.lastError )
console.log(chrome.extension.lastError);
else
myNamespace.tabs[package.modul] = tab.id;
chrome.tabs.onRemoved.addListener(
function( tabId, removeInfo )
{ myNamespace.tab[package.modul] = null; }
);
}
);
}
break;
}
}
)