MCPcopy Index your code
hub / github.com/hoothin/UserScripts / updateRules

Function updateRules

Pagetual/pagetual.user.js:10535–10660  ·  view source on GitHub ↗
(success, fail, keepCache, forceWedataMirrorFallback)

Source from the content-addressed store, hash-verified

10533 }
10534
10535 function updateRules (success, fail, keepCache, forceWedataMirrorFallback) {
10536 if (!storage.supportCrossSave()) {
10537 fail({url:''}, "Not support cross storage");
10538 showTips("Current platform do not support cross storage!");
10539 return;
10540 }
10541 if (inUpdate) return;
10542 inUpdate = true;
10543 let ruleIndex = ruleUrls.length - 1;
10544 if (!keepCache) {
10545 storage.setItem("hpRules", []);
10546 storage.setItem("smartRules", []);
10547 }
10548 let allOk = true;
10549 let preLength = ruleParser.rules.length;
10550 let fetchVersion = -1;
10551 let triedWedataMirrorFallback = false;
10552 function switchToWedataMirrorOnFail(rule) {
10553 if (!rule || rule.id !== 1 || rulesData.wedata2github || (updateDate && !forceWedataMirrorFallback)) {
10554 return false;
10555 }
10556 rulesData.wedata2github = true;
10557 storage.setItem("rulesData", rulesData);
10558 if (wedata2githubInputRef) {
10559 wedata2githubInputRef.checked = true;
10560 }
10561 if (ruleUrls && ruleUrls.length > 0) {
10562 for (let i = 0; i < ruleUrls.length; i++) {
10563 if (ruleUrls[i].id === 1) {
10564 ruleUrls[i].url = wedataMirrorRulesUrl;
10565 break;
10566 }
10567 }
10568 }
10569 rule.url = wedataMirrorRulesUrl;
10570 return true;
10571 }
10572 async function needUpdate(url, id) {
10573 if (!/^https:\/\/hoothin\.github\.io\/|\/hoothin\/UserScripts\/.*\/Pagetual\/pagetualRules\.json/i.test(url)) {
10574 return true;
10575 }
10576 let rulesLength = ruleParser.rules.reduce((acc, cur) => acc + (cur.from == id ? 1 : 0), 0);
10577 if (rulesLength === 0) return true;
10578 if (fetchVersion >= 0) {
10579 return fetchVersion > (rulesData.ruleVersion || 0);
10580 }
10581 fetchVersion = await new Promise(resolve => {
10582 _GM_xmlhttpRequest({
10583 url: url.replace(/\/\w+\.json/i, "/version"),
10584 method: 'GET',
10585 timeout: 20000,
10586 headers: {
10587 'accept': 'application/json,text/html'
10588 },
10589 onload: function(res) {
10590 let version = parseInt(res.response || res.responseText);
10591 resolve(version || -1);
10592 },

Callers 3

createImportBtnFunction · 0.85
initConfigFunction · 0.85
initRulesFunction · 0.85

Calls 2

showTipsFunction · 0.85
addNextRuleFunction · 0.85

Tested by

no test coverage detected