分享我也看不太懂的 Javascript / jQuery 副程式, 內容索引:
每2.5秒最多可以刷新次數:
這個是要解決短時間內大量的重覆重新整理頁面,可能會被售票系統封鎖,以往重新整理語法:
location.reload();
把上面一行,換成一下面這幾行:
var user_auto_reload_page_interval = 0.1; // 自動刷新頁面間隔(秒)
var user_auto_reload_overheat_count = 3; // 每2.5秒最多可以刷新次數
var user_auto_reload_overheat_cd = 2.0; // 達成最多刷新次數的冷確時間(秒)
async function do_reload_if_not_overheat(user_auto_reload_page_interval, auto_reload_overheat_count, auto_reload_overheat_cd) {
const overheat_second = 2.5;
const overheat_second_target = overheat_second * 1000;
let auto_reload_page_interval = user_auto_reload_page_interval;
chrome.storage.local.get('last_reload_timestamp', function(items) {
if (items.last_reload_timestamp) {
let new_timestamp = [];
const now = new Date().getTime();
//for (let i = items.last_reload_timestamp.length - 1; i >= 0; i--) {
for (let i = 0; i < items.last_reload_timestamp.length; i++) {
let each_time = items.last_reload_timestamp[i];
let current_diff = now - each_time;
if (current_diff <= overheat_second_target) {
//last_reload_timestamp.splice(i, 1);
new_timestamp.push(each_time);
}
}
new_timestamp.push(now);
chrome.storage.local.set({
last_reload_timestamp: new_timestamp
});
if (new_timestamp.length >= auto_reload_overheat_count) {
console.log("overheat, slow down!");
auto_reload_page_interval = auto_reload_overheat_cd;
}
const target_delay = auto_reload_page_interval * 1000;
//console.log("target_delay:" + target_delay);
if (target_delay < 100) {
//console.log('Start to reload now.');
location.reload();
} else {
//console.log('We are going to reload after few seconeds.');
setTimeout(function() {
location.reload();
}, target_delay);
}
} else {
console.log("initial timestamp.");
chrome.storage.local.set({
last_reload_timestamp: []
});
}
});
}
// 呼叫主程式:
// 如果這個主程式外面有 yourInterval = setInterval(() => { }, 100); 要先 clear
if (yourInterval) clearInterval(yourInterval);
do_reload_if_not_overheat(user_auto_reload_page_interval, user_auto_reload_overheat_count, user_auto_reload_overheat_cd);
在上面的程式執行之前,請找地方初始化變數內容:
chrome.storage.local.set({
last_reload_timestamp: []
});
這種太燒腦的程式,寫完就會開始看不懂自己寫了什麼。