Automatic Reload on Install
When a script is injected in a safari extension it runs on page load, so that means when an extension is installed the injected javascript will only apply to newly-opened pages or pages which are reloaded afterwards. Does anyone know 开发者_运维技巧how to ensure the javascript is run without having to reload?
Alternatively, forcing users to load... this seems a bit dangerous though.
I've also tried to find a way to "execute" or "attach" a script on install. This is possible in Chrome and Firefox, without having to reload the page. Unfortunately in Safari I haven't found a way to do this without having to reload the page and let the normal addContentScript handlers take care of injecting the script.
To reload all Safari tabs directly after extension has been installed:
/* In glabal.js */
// Reloads all tabs
function reloadTabs() {
var browserWindows = safari.application.browserWindows;
for (var i = 0; i < browserWindows.length; i++) {
var tabs = browserWindows[i].tabs;
for (var j = 0; j < tabs.length; j++) {
tabs[j].url = tabs[j].url;
}
}
}
// Called on first run
function onInstall() {
reloadTabs();
}
var firstRun = localStorage['extensionHasPreviouslyRun'] === undefined ||
!JSON.parse(localStorage['extensionHasPreviouslyRun']);
if (firstRun) {
onInstall();
localStorage['extensionHasPreviouslyRun'] = JSON.stringify(true);
}
精彩评论