如何只运行一次Greasemonkey脚本

编程入门 行业动态 更新时间:2024-10-08 04:34:16
本文介绍了如何只运行一次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 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脚本

本文发布于:2023-11-24 00:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623359.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   Greasemonkey

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!