最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Userscript should run once, not multiple times - Stack Overflow

programmeradmin3浏览0评论
// ==UserScript==
// @name         Test
// @namespace    /
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        /*
// @require .4.1.slim.min.js
// @grant        none
// ==/UserScript==
var index = 0;
(function() {
    'use strict';
    $(document).ready(function () {
        console.log(index);
        index++;
        setTimeout(() => {  console.log(index); }, 2000);
    });
})();

So has you can see this code should return in the console : 0 then 1 but the result is different, actually the script run multiple times.

My only clue is that come from the site in question, any idea ?

Excepted : (low rep image 1)

Actual : (low rep image 2)

// ==UserScript==
// @name         Test
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://myanimelist.net/*
// @require http://code.jquery.com/jquery-3.4.1.slim.min.js
// @grant        none
// ==/UserScript==
var index = 0;
(function() {
    'use strict';
    $(document).ready(function () {
        console.log(index);
        index++;
        setTimeout(() => {  console.log(index); }, 2000);
    });
})();

So has you can see this code should return in the console : 0 then 1 but the result is different, actually the script run multiple times.

My only clue is that come from the site in question, any idea ?

Excepted : (low rep image 1)

Actual : (low rep image 2)

Share Improve this question edited Jan 18, 2020 at 0:55 ButchMonkey 1,89920 silver badges30 bronze badges asked Jan 17, 2020 at 21:36 SadPandaSadPanda 1651 silver badge8 bronze badges 4
  • 1 Runs fine for me with and without using tampermonkey. Are you sure you don't have multiple instances of the script? – ButchMonkey Commented Jan 17, 2020 at 21:46
  • Without test, I see an IIFE plus the ready( event. Using slow internet, jquery is not loaded yet (header cache change not yet suceed 200OK), but the IIFE has already tigger since it's pure javascript. – NVRM Commented Jan 18, 2020 at 1:10
  • 2 The site may have iframes which you can test in devtools. Add // @noframes in metablock comment. Also you don't need jquery here at all and neither the $(document).ready wrapper. – woxxom Commented Jan 18, 2020 at 4:22
  • 2 // @noframes did the job ty ! (Solved) – SadPanda Commented Jan 20, 2020 at 5:26
Add a comment  | 

2 Answers 2

Reset to default 21

your script could be loading for each frame in the page.

add // @noframes to prevent this.

// ==UserScript==
// @name         Test
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://myanimelist.net/*
// @require http://code.jquery.com/jquery-3.4.1.slim.min.js
// @grant        none
// @noframes
// ==/UserScript==

you can use Lodash to run a function once

_.once( //Function to run only once )

发布评论

评论列表(0)

  1. 暂无评论