如何只运行一次Greasemonkey脚本 [英] How to Run a Greasemonkey Script only once

查看:111
本文介绍了如何只运行一次Greasemonkey脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为域创建了一个单一的脚本,现在如何使其仅运行一次?就像它在每次访问域时都开始一样,所以我不希望这样.如何使其仅运行一次,然后像删除自身或使其不活动?

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     http://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脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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