我为域创建了一个单一的脚本,现在如何使其仅运行一次?就像它在每次访问域时都开始一样,所以我不希望这样.如何使其仅运行一次,然后像删除自身或使其不活动?
I made a greasemonkey script for a domain, Now how do I make it run only once? Like it starts every time the domain is accessed, I don't want that. How do I make it run only once and then like delete itself or make itself inactive?
谢谢.
推荐答案如果您希望脚本(或脚本的一部分)仅运行一次,则需要将标志存储在非易失性存储中.脚本无法删除自身或关闭自身.但是它可以立即退出,而不是运行任何其他代码.
If you want a script (or part of a script) to run only once, then you need to store a flag in non-volatile storage. A script cannot delete itself or turn itself off. But it can exit right away rather than run any other code.
要基于站点(域)存储内容,请使用 localStorage .要基于脚本+命名空间存储内容,请使用 GM_setValue .这是一个基本脚本:
To store things on a site-by-site (domain) basis, use localStorage. To store things on a script+namespace basis, use GM_setValue. Here's a basic script:
// ==UserScript== // @name _Run a GM script one-time only // @namespace _pc // @include YOUR_SERVER.COM/YOUR_PATH/* // @grant GM_getValue // @grant GM_setValue // ==/UserScript== var alreadyRun = GM_getValue ("alreadyRun", false); if ( ! alreadyRun) { GM_setValue ("alreadyRun", true); alert ("This part of the script will run exactly once per install."); }更多推荐
如何只运行一次Greasemonkey脚本
发布评论