Benutzer:Theway-source/utilities.js
Hinweis: Leere nach dem Veröffentlichen den Browser-Cache, um die Änderungen sehen zu können.
- Firefox/Safari: Umschalttaste drücken und gleichzeitig Aktualisieren anklicken oder entweder Strg+F5 oder Strg+R (⌘+R auf dem Mac) drücken
- Google Chrome: Umschalttaste+Strg+R (⌘+Umschalttaste+R auf dem Mac) drücken
- Internet Explorer/Edge: Strg+F5 drücken oder Strg drücken und gleichzeitig Aktualisieren anklicken
- Opera: Strg+F5
//++++++++ "constants" ++++++++
var STRINGSEPARATOR = "|",
STORECOOKIE = 1,
STORELOCAL = 2,
STORESERVER = 3,
STORESTART = 11,
STOREEND = 12,
STORESINGLEKEY = 21, // for storing on wikimedia
STOREMULTIKEY = 22, // for storing on wikimedia
WMOPTIONPREFIX = "userjs-",
WMPREFERENCESNAME = "userjs-preferences";
var goPreferences = {}, // object with ALL preferences pairs
gsUserLang = "de",
gsStore = "", // summarized string with all data to store
goReceived;
//---- initialize the preferences ----
function PreferencesIni () {
Debug ("-- PreferencesIni goPreferences: " + $.isEmptyObject(goPreferences));
console.log ("-- PreferencesIni goPreferences: " + $.isEmptyObject(goPreferences));
ReceiveDataWikimedia (RunScripts); // load the preferences
}
//---- execute the scripts if preference is on ----
function RunScripts (oPreferences) {
Debug("-- RunScripts --");
console.log("-- RunScripts --");
console.log ("-- RunScripts goPreferences: " + $.isEmptyObject(goPreferences));
console.log("-- RunScripts oPreferences.mapframe_enable: " + oPreferences.mapframe_enable);
console.log("-- RunScripts oPreferences.mapframe_enable: " + oPreferences["mapframe_enable"]);
if (oPreferences.mapframe_enable === true)
mw.loader.load("//de.wikivoyage.org/w/index.php?title=Benutzer:Theway-source/mapframe.js&action=raw&ctype=text/javascript");
if (oPreferences.collapse_enable === true)
mw.loader.load("//de.wikivoyage.org/w/index.php?title=Benutzer:Theway-source/section-collapse.js&action=raw&ctype=text/javascript");
if (oPreferences.movesection_enable === true)
mw.loader.load("//de.wikivoyage.org/w/index.php?title=Benutzer:Theway-source/section-move.js&action=raw&ctype=text/javascript");
console.log("-- RunScripts oPreferences.renamesection_enable: " + oPreferences.renamesection_enable);
console.log("-- RunScripts oPreferences.renamesection_enable: " + oPreferences["renamesection_enable"]);
if (oPreferences.renamesection_enable === true)
mw.loader.load("//de.wikivoyage.org/w/index.php?title=Benutzer:Theway-source/section-rename.js&action=raw&ctype=text/javascript");
if (oPreferences.iwtooltip_enable === true)
mw.loader.load("//de.wikivoyage.org/w/index.php?title=Benutzer:Theway-source/wikidata.js&action=raw&ctype=text/javascript");
if (oPreferences.translate_enable === true)
mw.loader.load("//de.wikivoyage.org/w/index.php?title=Benutzer:Theway-source/translate.js&action=raw&ctype=text/javascript");
}
//++++++++ memorize of data ++++++++
//---- set a default value ----
// return: value of the cookie
function Default (sName, sValue) {
var sCookie, sEmpty = "whs-shw";
sCookie = GetCookie(sName, sEmpty);
if (sCookie == sEmpty) {
SetCookie (sName, sValue);
sCookie = sValue;
}
return sCookie;
}
//---- set a cookie, this function is only for definition by user ----
function Set (sName, sValue) {
return SetCookie (sName, sValue);
}
//---- set a cookie ----
function SetCookie (sName, sValue) {
var oDate = new Date();
var iUntil = new Date(oDate.getTime() + 10*365 * 60*60*24);
document.cookie = sName + "=" + sValue +";expires=" + iUntil.toGMTString()+";";
}
//---- get a cookie ----
function GetCookie (sName, sDefault) {
var sValue = "", iBeg = -1, iEnd;
if (document.cookie) {
iBeg = document.cookie.indexOf (sName + "="); // found sName
if (iBeg >= 0) {
iBeg = document.cookie.indexOf ("=", iBeg) + 1;
iEnd = document.cookie.indexOf (";", iBeg);
if (iEnd == -1) // don't set a semicolon at the end
iEnd = document.cookie.length;
sValue = document.cookie.substring (iBeg, iEnd);
}
else sValue = sDefault;
}
else sValue = sDefault;
return sValue;
}
//---- get a value somewhere ----
function GetValue (iLocation, sName, sDefault, xPreferences) {
Debug ("-- GetValue --");
Debug ("GetValue() iLocation: " + iLocation + ", sName: " + sName);
switch (iLocation) {
case STORELOCAL:
return GetValueLocal (sName, sDefault);
case STORESERVER:
Debug ("GetValue() xPreferences[sName]: " + xPreferences[sName]);
return xPreferences[sName];
default: // == STORECOOKIE
return GetCookie (sName, sDefault);
}
}
//---- store a value somewhere ----
function StoreValue (iLocation, sName, sValue) {
switch (iLocation) {
case STORELOCAL:
StoreValueLocal (sName, sValue);
break;
case STORESERVER:
gsStore += WMOPTIONPREFIX + sName + "=" + sValue + "|";
break;
case STORESTART:
gsStore = "";
break;
case STOREEND:
if (gsStore !== "")
StoreDataWikimedia ("", gsStore);
gsStore = "";
break;
default: // == STORECOOKIE
SetCookie (sName, sValue);
}
}
//---- store a value local (HTML5) ----
function StoreValueLocal (sName, sValue) {
if (typeof(Storage) != "undefined") // check browser support
localStorage.setItem(sName, sValue);
else
SetCookie (sName, sValue);
}
//---- get a value local (HTML5) ----
function GetValueLocal (sName, sDefault) { // todo: sDefault at localStorage too
if (typeof(Storage) != "undefined") // check browser support
return localStorage.getItem(sName);
else
GetCookie (sName, sDefault);
}
//---- get the user options from the wikimedia server ----
// see: https://www.mediawiki.org/wiki/API:Userinfo
// https://en.wikipedia.org/w/api.php?action=query&meta=userinfo&uiprop=options
// ProcessingFunction (xPreferences), xPreferences = pairs key:value
function ReceiveDataWikimedia (ProcessingFunction) {
Debug ("-- ReceiveDataWikimedia 1 --");
jQuery.ajax({
url: "/w/api.php", // will be automatic completed with https://de.wikivoyage.org
data: {
action: "query",
meta: "userinfo",
format: "json",
uiprop: "options",
},
type: "POST",
dataType: "json",
async: false,
success: function (oGet) {
if (typeof (oGet.query) === undefined) {
Debug ("ReceiveDataWikimedia: warning");
DebugObject(oGet);
return "undefined";
}
else {
try {
//console.log(oGet.query.userinfo.options[WMPREFERENCESNAME]);
goPreferences = JSON.parse(oGet.query.userinfo.options[WMPREFERENCESNAME]);
}
catch(err) {
Debug ("-- ReceiveDataWikimedia parse error ");
//console.log ("-- ReceiveDataWikimedia error, Key: " + sKey);
}
finally {
ProcessingFunction (goPreferences);
//Debug ("-- ReceiveDataWikimedia 2.5 --");
//console.log ("-- ReceiveDataWikimedia 2.5 --");
}
}
},
error: function() {
Debug ("ReceiveDataWikimedia: query error");
return undefined;
}
});
Debug ("-- ReceiveDataWikimediaIni ready --");
}
//---- store data to the wikimedia server ----
//see: https://www.mediawiki.org/wiki/API:Options
//see: https://en.wikipedia.org/wiki/Special:ApiSandbox#action=options&format=json&change=hideminor%3D1&optionname=userjs-test&optionvalue=22555&token=bc00b56ded545f6324911f0f957696a455ad4fd3
function StoreDataWikimedia (sName, sValue, iValueType) {
Debug ("-- StoreDataWikimedia --");
GetWikimediaToken (StoreDataWikimedia2, sName, sValue, iValueType);
}
//---- store data to the wikimedia server ----
// check result in: https://de.wikivoyage.org/w/api.php?action=query&meta=userinfo&uiprop=options
// see: https://www.mediawiki.org/wiki/API:Options
// see: https://en.wikipedia.org/wiki/Special:ApiSandbox#action=options&format=json&change=hideminor%3D1&optionname=userjs-test&optionvalue=22555&token=bc00b56ded545f6324911f0f957696a455ad4fd3
function StoreDataWikimedia2 (sToken, sName, sValue, iValueType) {
var sValueSingle = "", sValueMulti = "";
Debug ("-- StoreDataWikimedia2 --");
Debug ("-- StoreDataWikimedia2 sToken: " + sToken);
Debug ("-- StoreDataWikimedia2 sName: " + sName + ", sValue: " + sValue);
if (iValueType === STOREMULTIKEY) {
sValueMulti = sValue;
sName = "";
}
else // == STORESINGLEKEY, default
sValueSingle = sValue;
$.ajax({
url: "/w/api.php",
data: {
action: "options",
format: "json",
change: sValueMulti, // contain pairs of optionname=optionvalue like "userjs-test=66666|userjs-test2=222"
optionname: sName,
optionvalue: sValueSingle,
token: sToken,
},
type: "POST",
dataType: "json",
success: function (oGet) {
if (typeof (oGet.warnings) === undefined) {
Debug ("StoreDataWikimedia: warning");
DebugObject(oGet);
return "undefined";
}
else {
DebugObject(oGet);
return oGet.query.tokens.csrftoken; // if missing value: return "undefined"
}
},
error: function() {
Debug ("StoreDataWikimedia2: error");
return undefined;
}
});
}
//---- get a token from the current wikimedia site ----
// see: https://www.mediawiki.org/wiki/API:Options
// ProcessingFunction must be: function (sToken, sName, sValue)
function GetWikimediaToken (ProcessingFunction, sName, sValue) {
Debug ("-- GetWikimediaToken --");
$.ajax({
url: "/w/api.php",
data: {
action: "query",
meta: "tokens",
format: "json",
},
type: "POST",
dataType: "json",
success: function (oGet) {
if (typeof (oGet.warnings) === undefined) {
Debug ("GetWikimediaToken: warning");
DebugObject(oGet);
return "undefined";
}
else {
ProcessingFunction (oGet.query.tokens.csrftoken, sName, sValue);
return oGet.query.tokens.csrftoken; // if missing value: return "undefined"
}
},
error: function() {
Debug ("GetWikimediaToken: error");
return undefined;
}
});
}
//---- give a alias name of a specified id ----
// xAlias: array with alias names
// sId: the element in xAlias with the name to find
// sName: the name to find
// sAliasID: the element in xAlias with the result name
function AliasName (xAlias, sId, sName, sAliasID) {
//Debug ("TLAliasName, xAlias.length: " + xAlias.length);
for (var i=0; i < xAlias.length; i++) {
if (xAlias[i][sId] == sName)
return xAlias[i][sAliasID];
}
return undefined; // if language not content of xAlias
}
//---- set a default value, if preference don't have a value ----
function PreferenceDefault (sKey, sDefault) {
if (typeof goPreferences[sKey] === "undefined")
goPreferences[sKey] = sDefault;
return goPreferences[sKey];
}
//---- get the specific element of an array where is array[sIndex1] == sValue1 ----
function GetArrayElement (xArray, sIndex1, sValue1) {
for (var i = 0; i < xArray.length; i++) {
if (xArray[i][sIndex1] === sValue1)
return xArray[i];
}
return undefined;
}
//++++++++ workarounds ++++++++
//---- e.g Firefox don't work with innerText ----
function SetInnerText (o, s) {
if (o.innerText === undefined)
o.textContent = s;
else
o.innerText = s;
}
//---- e.g Firefox don't work with innerText ----
function GetInnerText (o) {
var s = o.textContent;
if (s === undefined) s = o.innerText;
if (s === undefined) s="";
return s;
}
//---- check is the event attribute available ----
//onbeforeunload not in opera
function IsEventSupported(sEvent) {
var oDummy = document.createElement('div');
var bIsSupported;
// check 1
bIsSupported = (sEvent in oDummy);
// check 2 if check 1 doesn't work
if (!bIsSupported) {
oDummy.setAttribute(sEvent, 'return;');
bIsSupported = (typeof oDummy[sEvent] == 'function');
}
oDummy = null;
return bIsSupported;
}
//---- store the position of a window into cookie ----
function StoreWindowPosition (oWindow, sCookie) {
//Debug ("StoreWindowPosition 1");
SetCookie (sCookie + "_width", oWindow.innerWidth);
SetCookie (sCookie + "_height", oWindow.innerHeight);
//Debug ("StoreWindowPosition 2");
SetCookie (sCookie + "_left", GetScreenLeft (oWindow));
SetCookie (sCookie + "_top" , GetScreenTop (oWindow));
//Debug ("StoreWindowPosition 3");
}
//---- e.g. old Opera don't work with screenX ----
//screenX is the real distance, ToDo: by screenLeft is to subtract the parent window position
function GetScreenLeft (oWindow) {
//Debug ("GetScreenLeft 1, screenLeft: " + oWindow.screenLeft + ", screenX: " + oWindow.screenX);
//Debug ("GetScreenLeft: " + (typeof oWindow.screenLeft == "number" ? oWindow.screenLeft : oWindow.screenX));
return (typeof oWindow.screenX == "number" ? oWindow.screenX : oWindow.screenLeft);
}
//---- e.g. old Opera don't work with screenX ----
//screenY is the real distance, ToDo: by screenTop is to subtract the parent window position
function GetScreenTop (oWindow) {
//Debug ("GetScreenLeft 1, screenTop: " + oWindow.screenTop + ", screenY: " + oWindow.screenY);
//Debug ("GetScreenTop: " + (typeof oWindow.screenTop == "number" ? oWindow.screenTop : oWindow.screenY));
return (typeof oWindow.screenY == "number" ? oWindow.screenY : oWindow.screenTop);
}
//---- get the next real html sibling ----
function getNextSibling (oFirst) {
var oNext = oFirst.nextSibling;
while (oNext !== null && oNext.nodeType != 1) {
oNext = oNext.nextSibling;
}
return oNext;
}
//++++++++ debug ++++++++
//---- debug messages into the logo title attribute ----
function Debug (sMessage) {
var oElement;
console.log (sMessage);
oElement = jQuery("#p-logo .mw-wiki-logo");
if (oElement.attr ("title") == "Hauptseite")
oElement.attr ("title", "Debug-Information: ");
oElement.attr ("title", oElement.attr ("title") + "\n" + sMessage);
}
//---- Debug an object ----
function DebugObject (o) {
Debug (JSON.stringify(o, null, 2));
}