User:JJPMaster/Scripts/OneClickWelcomer.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:JJPMaster/Scripts/OneClickWelcomer. |
mw.loader.load( `//en.wikipedia.org/w/index.php?title=User:${mw.config.get("wgUserName")}/OneClickWelcomerGlobalPreferences.js&action=raw&ctype=text/javascript` );
mw.loader.load( `//${mw.config.get("wgServer")}/w/index.php?title=User:${mw.config.get("wgUserName")}/OneClickWelcomerPreferences.js&action=raw&ctype=text/javascript` );
if (typeof globalAllAreWelcome == "undefined") globalAllAreWelcome = true;
if (typeof localAllAreWelcome == "undefined") localAllAreWelcome = true;
// if (typeof globalOCW == "undefined") globalOCW = true;
function welcome(username) {
var OCWparams;
var api;
if (isIP(username) && (!globalAllAreWelcome || !localAllAreWelcome)) {
OCWparams = {
action: 'edit',
title: "User talk:" + username,
appendtext: "\n{" + "{subst:Welcome-anon}} ~~" + "~~",
summary: "Welcomed new anonymous user with [[w:en:User:JJPMaster/OneClickWelcomer.js|OneClickWelcomer]]",
format: 'json'
},
api = new mw.Api();
}
else {
OCWparams = {
action: 'edit',
title: "User talk:" + username,
appendtext: "\n{" + "{subst:Welcome}} ~~" + "~~",
summary: "Welcomed new user with [[w:en:User:JJPMaster/OneClickWelcomer.js|OneClickWelcomer]]",
format: 'json'
},
api = new mw.Api();
}
api.postWithToken( 'csrf', OCWparams ).done( function ( data ) {
console.log( data );
} );
mw.notify($(`<a target="_blank" style="color: black; text-decoration: none;" href="/wiki/User_talk:${username}">Successfully welcomed the user. Click here to redirect to the user talk page.</a>`));
}
function isIP(username) {
const regex = /(?:^(?:25[0-5]|2[0-4]\d|1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d]{1,4}){0,5}:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-fA-F\d]{1,4}){1,7}|:)))(?:%[0-9a-zA-Z]{1,})?$)/gm;
if (regex.test(username)) {
return true;
}
return false;
}
// From "What is Object Equality in JavaScript" on GFG
function shallowEqual(obj1, obj2) {
const keys1 = Object.keys(obj1);
const keys2 = Object.keys(obj2);
if (keys1.length !== keys2.length) {
return false;
}
for (let key of keys1) {
if (!obj2.hasOwnProperty(key) || obj1[key] !== obj2[key]) {
return false;
}
}
return true;
}
// Adapted from [[User:BrandonXLF/Restorer.js]]
$(() => {
function OCWaddLink(item) {
var links = item.querySelector('.mw-usertoollinks');
var parent = document.createElement('span'),
el = document.createElement('a');
el.addEventListener('click', function() {
el.className = 'welcomer-loading';
welcome(getUser(parent));
});
el.textContent = 'welcome';
parent.appendChild(el);
if (links !== null)
links.appendChild(parent);
}
var parents = document.querySelectorAll('[data-mw-revid]');
function getUser(parent) {
return $(parent).closest("[data-mw-revid]").find("a.mw-userlink").find("bdi").text();
}
function OCWalternateAddLink() {
mw.util.addPortletLink('p-tb', '#', 'Welcome', 'OCWwelcome', 'Welcome this user');
$("#OCWwelcome").click(() => {
welcome(mw.config.get("wgRelevantUserName"));
});
}
if(mw.config.get("wgDBname") == "enwikibooks") {
throw new Error("Global OneClickWelcomer has been locally disabled.");
}
else {
parents.forEach((e) => OCWaddLink(e));
if(mw.config.get("wgCanonicalNamespace") == "User" || mw.config.get("wgCanonicalNamespace") == "User_talk" || mw.config.get("wgCanonicalSpecialPageName") == "Contributions") {
OCWalternateAddLink();
}
}
});