Firefox WebExtension:禁用/卸载之前如何运行代码? [英] Firefox WebExtension: How Do I Run Code Prior to Disable/Uninstall?

查看:55
本文介绍了Firefox WebExtension:禁用/卸载之前如何运行代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近将我的GreaseMonkey脚本转换为WebExtension,只是为了获得对该过程的第一印象.现在我已经达到了一个点,当禁用/卸载该扩展程序时,最好进行一些清理或简单地撤消所有更改.

I have recently converted a GreaseMonkey script of mine into a WebExtension, just to get a first impression of the process. Now I have a reached a point where it would be nice to do some clean-up or simply undo all my changes when said extension is disabled/uninstalled.

根据我在Mozilla页面上看到的内容, runtime.onSuspend 应该可以解决问题.不幸的是,它似乎尚未实现(我在Firefox常规发行版中).

From what I've seen on Mozilla's pages, runtime.onSuspend should do the trick. Unfortunately, it looks like that's not yet implemented (I'm on the regular Firefox release channel).

换句话说,我要执行的操作是运行代码,因为用户删除/禁用了我的扩展程序,因此我可以清理侦听器等,并通常将选项卡还原到它们的状态,即. e.,撤消扩展程序所做的所有更改.

In other words, what I want to do is run code as a result of the user removing/disabling my extension so that I can clean-up listeners and such and generally restore the tabs to their status quo, i. e., undo all the changes the extension made.

推荐答案

您最初的措词不清楚您想要的是什么.因此,此答案还包含有关在某些情况下可以接收到卸载通知的一种方式的信息.

Your initial wording was somewhat unclear as to exactly what you desire. Thus, this answer also contains information on one way you could receive a notification of the uninstall, under some conditions.

在卸载/禁用WebExtension插件之前运行代码:
不,即使受支持, runtime.onSuspend 事件无法满足您的需求.它用于通知事件页面即将被卸载.当页面正在监听的事件处理完成时,即使页面也会按常规方式卸载.这并不表示该扩展程序已被卸载.

Run code in your WebExtension add-on prior to uninstall/disable:
No, even if it was supported, the runtime.onSuspend event would not do what you want. It's used to signal Event Pages that they are about to be unloaded. Even Pages are unloaded routinely when the handling of the events they are listening for has completed. It is not an indication that the extension is being uninstalled.

确定"您的"WebExtension已被禁用/卸载":
如果您的问题确实是您在问题的最后一行中陈述的内容:"...是否可以确定是否禁用/卸载了WebExtension?"然后,您似乎可以使用 runtime.setUninstallURL() ,该版本自Firefox 47开始实施.这将允许您设置卸载附件时要访问的URL.可以在您的服务器上使用它来指出该加载项已卸载.它不会通知WebExtension它已被卸载,也不会允许您在WebExtension中运行代码.

"Determine" that your "WebExtension was disabled/uninstalled":
If your question is really what you state in the last line of your question: "... is there a way to determine whether a WebExtension was disabled/uninstalled?" Then, it looks like you could use runtime.setUninstallURL(), which was implemented as of Firefox 47. This will allow you to set a URL to visit when the add-on is uninstalled. This could be used on your server to note that the add-on was uninstalled. It does not inform your WebExtension that it was uninstalled, nor permit you to run code in your WebExtension when that happens.

不幸的是,您不能使用在WebExtension中检测到已访问此URL来表明您的WebExtension已被卸载/禁用.根据测试,在完全卸载WebExtension之后,将在 处访问此URL.此外,在禁用WebExtension时也不会访问它,在禁用WebExtension时也不会对其进行访问.仅在启用了加载项的情况下卸载WebExtension时,才可以访问它.由于这是一个仅在启用扩展名时运行的JavaScript调用,因此,人们希望仅在离开启用状态时才打开页面.

Unfortunately, you can not use detecting, in your WebExtension, that this URL was visited as indicating your WebExtension is being uninstalled/disabled. Based on testing, this URL is visited after the WebExtension has been completely uninstalled. In addition, it is not visited upon the WebExtension being disabled, nor when uninstalled after being disabled. It is only visited when the WebExtension is uninstalled while the add-on is enabled. From the fact that this is a JavaScript call which is only run when the extension is enabled, one would expect that the page would only be opened when leaving the enabled state.

通过将以下行添加到WebExtension并查看何时打开页面来完成测试:

Testing was done by adding the following line to a WebExtension and seeing when the page was opened:

chrome.runtime.setUninstallURL("http://www.google.com");

鉴于此功能的实际运行方式(仅在启用并直接卸载WebExtension的情况下才能访问),将其用作确定是否禁用/卸载WebExtension的方法"仅会部分有效.应当清楚的是,如果在卸载之前禁用了加载项,则不会通过访问此URL来通知您.

Given how this actually functions (only visited if the WebExtension is enabled and directly uninstalled), using this as "a way to determine whether a WebExtension was disabled/uninstalled" will only be partially effective. As should be clear, you will not be notified by a visit to this URL if the add-on is disabled prior to being uninstalled.

这篇关于Firefox WebExtension:禁用/卸载之前如何运行代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆