admin管理员组文章数量:1317909
I'm using javascript to create some automation action in a website. I know that chrome console would be reset when we hit the refresh button, so I'm trying to manage it by Tampermonkey so the script can run individually from the chrome console.
But my issue still there, tampermonkey userscript still reset when page is reloaded. Can you help me with those problem? Many thanks! p/s I'm an amateur in this coding subject so it is grateful if you guys can give me a hand :)
A part of my script is look like this:
var $balance = $("#balance"),
$statusBar = $(".progress #banner");
function getStatus() {
var a = $statusBar.text();
if (hasSubString(a, "Rolling in")) return "waiting";
if (hasSubString(a, "***ROLLING***")) return "rolling";
if (hasSubString(a, "rolled")) {
var b = parseInt(a.split("rolled")[1]);
return lastRollColor = getColor(b), "rolled"
}
return "unknown"
}
function getBalance() {
return parseInt($balance.text())
}
I'm using javascript to create some automation action in a website. I know that chrome console would be reset when we hit the refresh button, so I'm trying to manage it by Tampermonkey so the script can run individually from the chrome console.
But my issue still there, tampermonkey userscript still reset when page is reloaded. Can you help me with those problem? Many thanks! p/s I'm an amateur in this coding subject so it is grateful if you guys can give me a hand :)
A part of my script is look like this:
var $balance = $("#balance"),
$statusBar = $(".progress #banner");
function getStatus() {
var a = $statusBar.text();
if (hasSubString(a, "Rolling in")) return "waiting";
if (hasSubString(a, "***ROLLING***")) return "rolling";
if (hasSubString(a, "rolled")) {
var b = parseInt(a.split("rolled")[1]);
return lastRollColor = getColor(b), "rolled"
}
return "unknown"
}
function getBalance() {
return parseInt($balance.text())
}
Share
Improve this question
asked Mar 28, 2016 at 1:40
Vũ NguyễnVũ Nguyễn
1512 gold badges2 silver badges5 bronze badges
2 Answers
Reset to default 7The TamperMonkey script will indeed also reset when the page is reloaded. However, you can save the state between reloads so that your script can continue where it left off.
You can use the GM_getValue
and GM_setValue
functions to store data in some Tampermonkey data store, so that it is available the next time the script runs. Note that you need to use the @grant
header to use those functions.
For example, the following script keeps the value of counter
between reloads:
// ==UserScript==
// @name HttpBin reload
// @namespace http://tampermonkey/
// @version 0.1
// @description Reload something a couple of times
// @author Sjoerd
// @match https://httpbin/get
// @grant GM_setValue
// @grant GM_getValue
// ==/UserScript==
(function() {
'use strict';
var i = GM_getValue('counter', 0);
GM_setValue('counter', i + 1);
if (i < 5) {
location.reload();
}
})();
You could try adding whatever it is you are trying to save to the localStorage
of the page and whenever you need whatever you just saved, you can just get it from the localStorage
.
Here are 2 mands to do so:
localStorage.setItem(keyName, keyValue);
localStorage.getItem(keyName);
For more info check these 2 links:
https://developer.mozilla/en-US/docs/Web/API/Storage/setItem
https://developer.mozilla/en-US/docs/Web/API/Storage/getItem
本文标签: javascriptHow to reload page without resetting tampermonkey scriptStack Overflow
版权声明:本文标题:javascript - How to reload page without resetting tampermonkey script? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742027740a2415866.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论