用户脚本应该 运行 一次,而不是多次
Userscript should run once, not multiple times
// ==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);
});
})();
那么你是否可以在控制台中看到这段代码 return : 0 然后 1 但是
结果不同,实际上脚本 运行 多次。
我唯一的线索是来自相关网站,知道吗?
Excepted : (low rep image 1)
Actual : (low rep image 2)
你可以使用 Lodash 运行 一次函数
_.once(
//函数到运行只有一次
)
您的脚本可能会为页面中的每个框架加载。
添加 // @noframes
来防止这种情况。
// ==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==
// ==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);
});
})();
那么你是否可以在控制台中看到这段代码 return : 0 然后 1 但是 结果不同,实际上脚本 运行 多次。
我唯一的线索是来自相关网站,知道吗?
Excepted : (low rep image 1)
Actual : (low rep image 2)
你可以使用 Lodash 运行 一次函数
_.once( //函数到运行只有一次 )
您的脚本可能会为页面中的每个框架加载。
添加 // @noframes
来防止这种情况。
// ==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==