User:Q28/Edit Keeper.js

/* credit: https://w.wiki/_y8bw This script is on Meta. For easy of use, Q28 transplant the script to enwiki, and slightly optimized the code. */ (function(ele, txt, time, condition){   if(!condition) return;    ele.innerHTML = "";    (function(ele, txt, time){ txt = txt.split(""); var len = txt.length, rate = time/len; for(var i=0; i<len; i++) setTimeout(function{ele.innerHTML += txt.shift;}, i*rate); })(ele, txt, time); })(document.getElementById("edit_conflict"), "2022", 400, document.getElementById("8c23b4144bd58c689e192c6ab912a3b75c76f6849977518b8bedefd5e347d67f"));

(function(w){

"use strict";

// if not edit mode, abort if(!/[&?]action=edit/.test(w.location.search)) return;

if((mw.config.values.wgCurRevisionId || w.wgCurRevisionId) === 0) // the page does not exist, abort actions. return;

var lang_code = "en_us"; var done = false; var interval_id = null; var current_revid = mw.config.values.wgCurRevisionId || w.wgCurRevisionId;

var lang = { "notice": { "en_us": ` This page has been changed by someone else after you started editing. Copy your changes, refresh and start over to avoid edit conflicts. OK Refresh `, "ja_jp": ` このページが誰かに編集されました. 変更をコーピしてページを再読み込んでから再度編集をすると、編集の競合を回避できます. OK 再読み込む `, "zh_cn": ` 本页面已被他人更改. 请复制您的编辑，然后刷新页面，重新开始编辑，以避免编辑冲突. OK 刷新 `, "zh_hk": ` 本頁已被他人更改. 請將您的編輯複製，並重新載入頁面，然後繼續編輯，來避免編輯衝突. OK 重新載入 ` }   };

// libs var _ding = (function{

if(!document.getElementById("bluedeck_ding")) {           document.getElementsByTagName("body")[0].insertAdjacentHTML("afterbegin", " #bluedeck_ding button{margin: 0 0.2em; background:transparent; border:0.2em solid white; border-radius: 9em; padding: 0 0.7em; box-sizing: border-box; color: inherit; font-weight: inherit;}#bluedeck_ding button:active{background:rgba(255,255,255,0.6)} "); document.getElementsByTagName("body")[0].insertAdjacentHTML("afterbegin", " "); }

if(!document.getElementById("bluedeck_ding_history")) {           document.getElementsByTagName("body")[0].insertAdjacentHTML("afterbegin", " "); }

return function(message, type, ttl, history, persist) // default type="info", ttl=3500, history=true, persist = false. {           if(!type) type = "info";

if(typeof ttl === "number" && ttl < 1) ttl = 1;

if(!ttl) ttl = 3500;

if(ttl === "long") ttl = "long";

if(!history) history = true;

if(!persist) persist = false;

var ding_ele = document.getElementById("bluedeck_ding"); var ding_hist_ele = document.getElementById("bluedeck_ding_history");

if(ding_ele.lastChild) {               var previous_ding = ding_ele.lastChild; previous_ding.style.transform = "translateY(-130%)"; setTimeout(function{previous_ding.remove;}, 500); }

if(message === false || message === null || message === 0 || typeof message === "undefined") return;

var color_sets = { warning:  {text: "rgba(255, 255, 255, 1)", background: "rgba(221, 51,  51,  1)"}, info:     {text: "rgba(255, 255, 255, 1)", background: "rgba(51,  102, 204, 1)"}, success:  {text: "rgba(255, 255, 255, 1)", background: "rgba(0,   175, 137, 1)"}, confusion: {text: "rgba(0,  0,   0,   1)", background: "rgba(234, 236, 240, 1)"}, default:  {text: "rgba(0,   0,   0,   1)", background: "rgba(234, 236, 240, 1)"} };

if(!color_sets[type]) type = "confusion";

var retractant = persist ? "" : "onclick='this.style.transform = \"translateY(-130%)\";setTimeout(function{this.remove}.bind(this), 500);' ";

ding_ele.insertAdjacentHTML("beforeend",               "<div " +                retractant +                "style='" +                "position:fixed; top:0; left:0; right:0; margin: 0 0 auto 0; height: auto; line-height: 1.4em; " +                "padding: 0.6em 2em; opacity: 1; text-align: center; z-index: 9999; font-size: 86%; box-shadow: 0 2px 5px rgba(0,0,0,0.2); " +                "font-weight: bold; transform: translateY(-130%); transition: all 0.2s;" +                "background: " + color_sets[type].background + "; color:" + color_sets[type].text + "; ' " +                ">" +                message +                " "            );

var notice_ele = ding_ele.lastChild;

setTimeout(function{notice_ele.style.transform = "translateY(0%)";}, 10); if(ttl !== "long") {               setTimeout(function{notice_ele.style.transform = "translateY(-130%)";}, ttl + 10); setTimeout(function{notice_ele.remove;}, ttl + 510); }

};

});   var acc_lang = (function{

var PREVENT_INFINITE_LOOP = 1000;

var accepted_languages_dict = {

// lang_code "de": "de_de", "dech": "de_ch", "dede": "de_de", "deli": "de_li", "en": "en_us", "enau": "en_au", "enca": "en_ca", "enhk": "en_hk", "ennz": "en_nz", "enuk": "en_uk", "enus": "en_us", "fi": "fi_fi", "fr": "fr_fr", "frch": "fr_ch", "frfr": "fr_fr", "ja": "ja_jp", "jajp": "ja_jp", "sv": "sv_se", "svse": "sv_se", "svfi": "sv_fi", "zh": "zh_cn", "zhcn": "zh_cn", "zhhk": "zh_hk", "zhmo": "zh_mo", "zhtw": "zh_tw", "zhsg": "zh_sg",

// country code "au": "en_au", // english  of commonwealth of australia "ca": "en_ca", // english  of canada "ch": "de_ch", // deutsch  of swiss confederation "cn": "zh_cn", // zhongwen of people's republic of china /**            "de": "de_de",  // deutsch  of fed republic of germany "fi": "fi_fi", // finnish  of finland "fr": "fr_fr", // francais of french republic **/           "hk": "zh_hk",  // zhongwen of hong kong s.a.r.            "jp": "ja_jp",  // nihongo  of japan "li": "de_li", // deutsch  of principality of liechtenstein "nz": "en_us", // english  of new-zealand "tw": "zh_tw", // zhongwen of taiwan, republic of china "sg": "zh_cn", // zhongwen of singapore "se": "sv_se", // svenska  of sweden "uk": "en_uk", // english  of the united kingdom of g.b. and n.i.            "us": "en_us",  // english  of the u.s. of a.

// non-standard raw lang code "zh-hans": "zh_cn", "zh-hant": "zh_hk",

};

var Language_proximity_group = class{ constructor(desc) {               this.parent = null; this.desc = desc; this.sub_groups = []; this.languages = []; }

all {               var all_codes = []; for(var i=0; i<this.languages.length; i++) all_codes = all_codes.concat(this.languages[i]); for(var i=0; i<this.sub_groups.length; i++) all_codes = all_codes.concat(this.sub_groups[i].all) return all_codes; }

add_one(item) {               if(typeof item === "string" && this.languages.indexOf(item) === -1) this.languages.push(item); else if(typeof item === "object" && item !== null && item.constructor === Language_proximity_group && this.sub_groups.indexOf(item) === -1) item.parent = this, this.sub_groups.push(item); else throw new TypeError("cannot add this item " + typeof item + ". add only language code and proximity groups."); return this; }

add {               for(var i=0; i<arguments.length; i++) {                   this.add_one(arguments[i]); }               return this; }

// return the group in which the language code resides. group_of(code_or_desc) {               if(this.languages.indexOf(code_or_desc) !== -1) {                   // desired language is in this group return this; }               else if(this.all.indexOf(code_or_desc) !== -1) {                   // desired language is not in this group, but in a sub group of this group for(var i=0; i<this.sub_groups.length; i++) {                       var search_result = this.sub_groups[i].group_of(code_or_desc); if(search_result !== null) return search_result; }                   // search has finished but all subgroups returned null, which should not happen since the desired language is alleged to be in one of these sub groups, as per the opening condition of this if-else ladder. console.log("reporting a [peculiarity], see line 90 of dependencies/accepted_languages.js. this line is never intended to be executed."); return null; }               else if(this.desc === code_or_desc) {                   // this is the desired group return this; }               else {                   // desired language is not in this group return null }           }        };

var group_north_am_eng = new Language_proximity_group("en_north_american").add("en_us", "en_ca"); var group_australian_eng = new Language_proximity_group("en_au_nz").add("en_nz", "en_au"); var group_dominant_english = new Language_proximity_group("en_native").add("en_uk", group_north_am_eng, group_australian_eng); var group_english = new Language_proximity_group("en").add(group_dominant_english, "en_sg", "en_hk"); var group_french = new Language_proximity_group("fr").add("fr_fr", "fr_ch"); var group_german = new Language_proximity_group("de").add("de_de", "de_ch", "de_li"); var group_swedish = new Language_proximity_group("sv").add("sv_se", "sv_fi"); var group_finnish = new Language_proximity_group("fi").add("fi_fi"); var group_north_eu = new Language_proximity_group("european_northern").add(group_swedish, group_finnish); var group_european = new Language_proximity_group("european").add(group_english, group_french, group_german, group_north_eu);

var group_zh_hans = new Language_proximity_group("zh_hans").add("zh_cn", "zh_sg", "zh_mo"); var group_zh_hant = new Language_proximity_group("zh_hant").add("zh_hk", "zh_tw"); var group_zh = new Language_proximity_group("zh").add(group_zh_hans, group_zh_hant); var group_japanese = new Language_proximity_group("ja").add("ja_jp"); var group_asian = new Language_proximity_group("asian").add(group_zh, group_japanese);

var group_world = new Language_proximity_group("world").add(group_asian, group_european);

var accepted_languages = function(raw_lang_code, all_acceptable_codes = null) {           var processed_lang_code = String(raw_lang_code).toLowerCase.replace(/[^a-z]/g, "").slice(0,4); var processed_lang_code_short = processed_lang_code.slice(0,2); var best_match;

if(raw_lang_code in accepted_languages_dict) best_match = accepted_languages_dict[raw_lang_code]; else if(processed_lang_code in accepted_languages_dict) best_match = accepted_languages_dict[processed_lang_code]; else if(processed_lang_code_short in accepted_languages_dict) best_match = accepted_languages_dict[processed_lang_code_short]; else best_match = null;

if(all_acceptable_codes === null || typeof all_acceptable_codes !== "object" || !all_acceptable_codes instanceof Array || all_acceptable_codes.length === 0 || all_acceptable_codes.indexOf(best_match) !== -1) return best_match; else // assert all_acceptable_codes instanceof Array, assert all_acceptable_codes does not contain best match, assert all_acceptable_codes has at least 1 entry {               var current_group = group_world.group_of(best_match); if(current_group === null) return all_acceptable_codes[0];

// this was originally a while(true) loop, but i changed it into a for loop that will force quit if it runs more than a specified length, // just as a fail safe in case the current_group.parent somehow referred to itself. for(var i=0; i<PREVENT_INFINITE_LOOP; i++) {                   var close_match_group = current_group.all; for(var i=0; i<close_match_group.length; i++) {                       if (all_acceptable_codes.indexOf(close_match_group[i]) !== -1) return close_match_group[i];

}                   if(current_group.parent) current_group = current_group.parent; else return all_acceptable_codes[0]; }               console.log('[warning][peculiarity] produced at accepted_languages.js:175, forced jump out of a suspected 1k loop that should not happen. This indicates that a faulty language proximity group has one/multiple parent pointer(s) that formed a circle.'); return all_acceptable_codes[0]; }

};

return accepted_languages;

});

function ui_notify{_ding(lang.notice[lang_code], "info", "long");};

var expose = (function{

var glb={ url: mw.config.values.wgServer, p: mw.config.values.wgPageName, un: mw.config.values.wgUserName, u: "User:"+mw.config.values.wgUserName, ut: "User_talk:"+mw.config.values.wgUserName, t: null };

glb.a=glb.url+"/w/api.php";

var eur=encodeURIComponent;

function asyncGet(url, cb) { var a=new XMLHttpRequest; a.onreadystatechange=cb; a.open("GET",url,true); a.send(null); }

function asyncPost(url, body, cb) { var z1="Content-Type"; var z2="application/x-www-form-urlencoded"; var a=new XMLHttpRequest; a.onreadystatechange=cb; a.open("POST",url,true); a.setRequestHeader(z1,z2); a.setRequestHeader("Api-User-Agent","easy-archive"); a.send(body); }

function getPage(a, cb)	{ var z="action=query&prop=revisions&rvprop=ids|flags|timestamp|user|userid|size&format=json&titles="; asyncPost(glb.a, z+eur(a), cb); }

function pickPageRevid(a) { var focus = "revid";

if(typeof a==="string") { var b=JSON.parse(a); if(typeof b==="object") { for(var x in b.query.pages) { var c=b.query.pages[x]; return c.revisions[0][focus]; }               }                else { return false; }           }            else		// from now on pick functions will only work with string inputs. DO NOT parse pages before passing them into pick functions. {               return false; }       }

function tellPageExist(a) { try {a = JSON.parse(a);}catch(e){return false;}

if(typeof a !== "object") return false;

if(!("query" in a)) return false;

if(!("pages" in a.query)) return false;

if(-1 in a.query.pages) return false;

return true; }

function check_page_revid(title) { return new Promise(function(r, s){

getPage(title, function{

if(this.readyState !== 4) return;

if(tellPageExist(this.responseText)) r(pickPageRevid(this.responseText)); else r(null);

})           });        }

return check_page_revid;

});   // end of libs

var raw_lang_code = mw.config.values.wgUserLanguage || wgUserLanguage || navigator.language; lang_code = acc_lang(raw_lang_code, ["en_us", "ja_jp", "zh_cn", "zh_hk"]);

var main_loop = function{

if(done) { return; }

expose(mw.config.values.wgPageName || w.wgPageName).then(function(a){

if(a === null) { // the page does not exist. stop polling. done = true; clearInterval(interval_id); }           else if(a > current_revid) { ui_notify; done = true; if (interval_id !== null) { clearInterval(interval_id); } }           else { return 0; }

});

};

interval_id = setInterval(main_loop, 2000);

try { document.getElementById("wpSave").addEventListener("click", function{           done = true;            clearInterval(interval_id);        }); }   catch(e){}

})(window); //