Reputation: 15045
I cannot find something like "closeTab" event in the Apple Documentation.
I've tried:
injected.js
window.addEventListener("unload", function(){
// Probably closed.
// Now I need to tell it to the global page.
}, false);
but I can't find a way to send a message from within an injected script to the global page. Messages and Proxies mentions only other way around.
Upvotes: 3
Views: 3500
Reputation: 243
For Safari 5.1 onwards, they added a tab close event that you can listen for and handle in the usual way.
Upvotes: 3
Reputation: 31
use the "validate" event in background to detect tab switches, for example
var popupBackground = {
initialised : false,
activeTab : null,
numberOfTabs : null,
_init : function() {
var that = this;
// on browser initialise reset data
localStorage.clear();
// this initialises the popup dialogue
localStorage["popupOpened"] = false;
// register listeners for application messaging
safari.application.addEventListener("command", function(event){
that.handleCommand(event);
}, false);
safari.application.addEventListener("validate",function(event){
that.validateCommand(event);
}, false);
safari.application.addEventListener("message", function(event){
that.handleMessage(event);
}, false);
},
_getActiveTab : function(){
return safari.application.activeBrowserWindow.activeTab;
},
// commands are validated before being excecuted
validateCommand : function(aEvent) {
var that = this;
// all commands should have the identifier specified in Extension Builder
if (aEvent.command === "togglePopup") {
// check that there is an active tab
if (!aEvent.target.browserWindow.activeTab.url) {
aEvent.target.disabled = true;
} else {
aEvent.target.disabled = false;
}
}
// this is a hack for detecting tab switches, safari does not have a dedicated API like Chrome
if(this.activeTab !== null){
if(this.activeTab !== this._getActiveTab()){
$.each(safari.application.browserWindows, function(aIndex, aWindow) {
$.each(aWindow.tabs, function(aIndex, aTab) {
if(typeof aTab.page !== "undefined"){
// message all tabs about the focus switch event
if (aTab !== that._getActiveTab()) {
aTab.page.dispatchMessage("tabUnfocused");
// set the popup status (incase tab closed with open popup)
localStorage["popupOpened"] = false;
}else{
aTab.page.dispatchMessage("tabFocused");
}
}
});
});
}
}
// set the new active tab
this.activeTab = this._getActiveTab();
}
}
Upvotes: 1
Reputation: 2829
So far I haven't found a way for a tab to tell the global page that it's been (or is about to be) closed. One crappy workaround is to set up a timer on the global page that will check periodically whether any tabs have been closed. The following simplistic code will log when a tab is closed in the active window:
var tabs = safari.application.activeBrowserWindow.tabs;
var myTimer = setInterval(function (){
for (var i = 0; i < tabs.length; i++) {
if (app.activeBrowserWindow.tabs[i] !== tabs[i]) {
console.log('A tab was closed.');
}
}
tabs = safari.application.activeBrowserWindow.tabs;
}, 1000);
This example is pretty useless, as it doesn't provide any information about the tab that was closed, and it will yield a false positive when a tab is merely moved.
Upvotes: 0
Reputation: 1
You can add listeners directly to the events, but so far I haven't found the correct one to detect when a tab is closed.
window.onload = function ( event ) {
safari.self.tab.dispatchMessage("onLoad","it's alive!!"); }
window.onfocus = function ( event ) {
safari.self.tab.dispatchMessage("onFocus","it's alive!!"); }
window.onblur = function ( event ) {
safari.self.tab.dispatchMessage("onBlur","it's alive!!"); }
window.onunload = function ( event ) {
safari.self.tab.dispatchMessage("onUnload","it's alive!!"); }
window.ondrop = function ( event ) {
safari.self.tab.dispatchMessage("onDrop","it's alive!!"); }
window.onpagehide = function ( event ) {
safari.self.tab.dispatchMessage("onPagehide","it's alive!!"); }
window.onpageshow = function ( event ) {
safari.self.tab.dispatchMessage("onPageshow","it's alive!!"); }
window.onbeforeunload = function ( event ) {
safari.self.tab.dispatchMessage("onBeforeunload","it's alive!!"); }
window.onchange = function ( event ) {
safari.self.tab.dispatchMessage("onChange","it's alive!!"); }
window.onemptied = function ( event ) {
safari.self.tab.dispatchMessage("onEmptied","it's alive!!"); }
window.onopen = function ( event ) {
safari.self.tab.dispatchMessage("onOpen","it's alive!!"); }
window.onended = function ( event ) {
safari.self.tab.dispatchMessage("onEnded","it's alive!!"); }
window.onerror = function ( event ) {
safari.self.tab.dispatchMessage("onError","it's alive!!"); }
Upvotes: 0
Reputation: 6653
You can dispatch a message from an injected script to a global page like so:
safari.self.tab.dispatchMessage("messageName", messageData);
(Your global page must have something to capture these events):
// register for message callbacks from the injected script
safari.application.addEventListener("message", respondToMessageFunction, false);
As far as how to capture "tab closed" events... your guess is as good as mine. I am actually trying to find that answer myself currently.
Upvotes: 2