Automatic restart during installation

When a script is entered into the safari extension, it starts when the page loads, so this means that when the extension is installed, the embedded javascript will only be applied to newly opened pages or pages that are then reloaded. Does anyone know how to ensure javascript execution without rebooting?

Alternatively, forcing users to log in ... it seems a bit dangerous, though.

+3
source share
1 answer

I also tried to find a way to "execute" or "attach" the script during installation. This is possible in Chrome and Firefox, without having to reload the page. Unfortunately, in Safari I did not find a way to do this without reloading the page, and let the normal addContentScript handlers take care of the script input.

To reload all Safari tabs immediately after installing the extension:

/* 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);
}
0
source

Source: https://habr.com/ru/post/1756975/


All Articles