安装时自动重新加载

发布于 2024-09-11 14:57:10 字数 146 浏览 4 评论 0原文

当脚本被注入到 safari 扩展中时,它会在页面加载时运行,这意味着当安装扩展时,注入的 javascript 将仅适用于新打开的页面或随后重新加载的页面。有谁知道如何确保 javascript 运行而无需重新加载?

或者,强迫用户加载...但这似乎有点危险。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏日浅笑〃 2024-09-18 14:57:10

我还尝试找到一种在安装时“执行”或“附加”脚本的方法。这在 Chrome 和 Firefox 中是可能的,无需重新加载页面。不幸的是,在 Safari 中,我还没有找到一种方法来做到这一点,而无需重新加载页面并让普通的 addContentScript 处理程序负责注入脚本。

要在安装扩展后直接重新加载所有 Safari 选项卡:

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文