We're seeing some behavior with Safari that's unique to Safari and not seeing it on other browsers... And that is that the background.js page seems to be re-initialized with every tab opening, rather than once per browser.
Do I have this right? This is causing us some problems as we're dependent upon background.js only being initialized once per browser, not per tab.
There is a section of the docs on background.js which says this:
Due to Firefox limitations, the background code is run once per browser instance.
I don't think the above is a limitation, I've thought that it was normal behavior for ALL browsers (not just Firefox), until we noticed that Safari is a little different.
Help get this topic noticed by sharing it on
Twitter,
Facebook, or email.
Twitter,
Facebook, or email.
-
Hello Bryan,
I unsuccessfully tried to reproduce the problem. In order to investigate this further, I need to take a closer look at the extension.
Therefore, please can you provide as much of the following information as possible and I will investigate:
1. The extension id
2. The file/line number where the code snippet can be found
3. Which browser/OS you are testing on
4. The exact steps for reproducing the problem
5. Any additional information such as URLs, videos, ... that will help identify the problem -
-
-
-
I have confirmed that in Safari, this problem occurs with
<a target=_blank>, but it does NOT occur with appURL.openURL({where: 'tab'}).
I've enhanced my sandbox extension #39967 to respond to events from the page to open a new tab with openURL. Try saving this HTML page to your web server (can't load it as file:// - must load it as http://), and then try the two methods for opening a new tab. The method will cause background.js to reload. The message method will not.
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="https://w9u6a2p6.ssl.hwcdn.net/plugins/javascripts/crossriderAPI.js"></script>
<script type=text/javascript>
function onMsg() {
$('body').fireExtensionEvent('newtab');
}
</script>
</head>
<body>
<a target="_blank" href="http://cnn.com">Hyperlink</a>
<br>
<br>
<a href="javascript:onMsg()">Message</a>
</body>
</html>
-
-
Hello Bryan,
Thanks for the additional information. I have passed it to the dev team to check and I'll get back to you once I hear back from them. -
-
Any update on this? We depend upon background.js having "statefulness", and at the moment, in Safari, it appears that background.js is getting re-initialized every time someone hits a hyperlink with a new target window (which opens a new tab).
-
-
Hello Bryan,
Even though this is in the dev team's queue, it may take them awhile to resolve as they are currently working on several projects.
As a workaround, I would suggest saving the stateful information you require to the local database appAPI.db.async as required and loading it at the start of background.js so that the reloaded/new instance has access to the stateful information. -
-
Shlomo,
I will try that. But I could use one more piece of inforamtion to make it all work - is there any way that you can think of in Safari, such that when background.js is initialized, I can detect whether it is because the user opened a new tab, or if it's because they just launched their browser? I can make your suggestion with db.async work, if I can also get this question answered (knowing if it's a fresh launch vs. a new tab). -
-
Hello Bryan,
In general, I can't think of any native ways to do it.
However, perhaps you can create a unique heartbeat per background and check it at the start of the script. Something like the following untested code:appAPI.ready(function() {
// Remove existing heartbeat
// Logic: If there is another background running,
// a new heartbeat will be created before this background starts its heartbeat
appAPI.db.remove('heartbeat');
// Wait 2 seconds before starting heartbeat
setTimeout(function() {
// If after 2 seconds no heartbeat is detected then it's a new browser
// otherwise it's a new tab
var newTab = appAPI.db.get('heartbeat') === null ? false : true;
// Set up a new heartbeat every second
setInterval(function() {
appAPI.db.set('heartbeat', true);
}, 1 * 1000);
}, 2 * 1000);
});
Obviously, you can play with the timings as required. -
-
-
Loading Profile...




EMPLOYEE
