User:JJPMaster/OneClickWelcomer/unstable.js
Appearance
Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
- Mozilla / Firefox / Safari: hold down Shift while clicking Reload, or press Ctrl-Shift-R (Cmd-Shift-R on Apple Mac);
- Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5;
- Konqueror: simply click the Reload button, or press F5;
- Opera users may need to completely clear their cache in Tools→Preferences.
Warning: Malicious code can compromise your account. Page preview will cause your web browser to execute this page's content as code under some skins, including Monobook. If you have any questions about any code you plan to add, you can ask at the appropriate reading room. |
Documentation for this user script can be added at User:JJPMaster/OneClickWelcomer/unstable. |
//<nowiki>
function welcomeToWikibooks(username) {
var OCWparams;
var api;
if (isIPwb(username)) {
OCWparams = {
action: 'edit',
title: "User talk:" + username,
appendtext: "\r\n== Welcome to Wikibooks! ==\r\n{{subst:Welcome-anon}} ~~~~",
summary: "Welcomed new anonymous user with [[User:JJPMaster/OneClickWelcomer.js|OneClickWelcomer]]",
format: 'json'
},
api = new mw.Api();
api.postWithToken( 'csrf', OCWparams ).done( function ( data ) {
console.log( data );
} );
}
else {
OCWparams = {
action: 'edit',
title: "User talk:" + username,
appendtext: "\r\n{{subst:Welcome}} ~~~~",
summary: "Welcomed new user with [[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 warn(username, level) {
var OCWparams;
var api;
if (isIPwb(username)) {
OCWparams = {
action: 'edit',
title: "User talk:" + username,
appendtext: `\r\n== {{subst:#time: F Y}} ==\r\n{{subst:TestAnon${level}}} ~~~~`,
summary: "Warned anonymous user with [[User:JJPMaster/OneClickWelcomer.js|OneClickWelcomer]]",
format: 'json'
},
api = new mw.Api();
api.postWithToken( 'csrf', OCWparams ).done( function ( data ) {
console.log( data );
} );
}
else {
OCWparams = {
action: 'edit',
title: "User talk:" + username,
appendtext: `\r\n== {{subst:#time: F Y}} ==\r\n{{subst:Test${level}}} ~~~~`,
summary: "Warned user with [[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 warned the user. Click here to redirect to the user talk page.</a>`));
}
function isIPwb(username) {
const regex = /(?:^(?: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}:){7}(?:[a-fA-F\d]{1,4}|:)|(?:[a-fA-F\d]{1,4}:){6}(?:(?: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}|:)|(?:[a-fA-F\d]{1,4}:){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,2}|:)|(?:[a-fA-F\d]{1,4}:){4}(?:(?::[a-fA-F\d]{1,4}){0,1}:(?: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,3}|:)|(?:[a-fA-F\d]{1,4}:){3}(?:(?::[a-fA-F\d]{1,4}){0,2}:(?: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,4}|:)|(?:[a-fA-F\d]{1,4}:){2}(?:(?::[a-fA-F\d]{1,4}){0,3}:(?: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,5}|:)|(?:[a-fA-F\d]{1,4}:){1}(?:(?::[a-fA-F\d]{1,4}){0,4}:(?: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,6}|:)|(?::(?:(?::[a-fA-F\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;
}
// Adapted from [[w:en:User:BrandonXLF/Restorer.js]]
$(() => {
function getUser(parent) {
return $(parent).closest("[data-mw-revid]").find("a.mw-userlink").find("bdi").text();
}
function OCWaddLink(item) {
var links = item.querySelector('.mw-usertoollinks');
var parent = document.createElement('span'),
el = document.createElement('a');
var parent2 = document.createElement('span'),
el2 = document.createElement('a');
el.addEventListener('click', function() {
el.className = 'welcomer-loading';
welcomeToWikibooks(getUser(parent));
});
el2.addEventListener('click', function(e) {
el2.className = 'warner-loading';
var level = prompt("What level? Type '1', '2', or '3' without the quotes. Default: 1", "1");
if (!level) return;
warn(getUser(parent), level);
});
el.textContent = 'welcome';
el2.textContent = 'warn';
parent.appendChild(el);
parent2.appendChild(el2);
if (links !== null)
links.append(parent, parent2);
}
var parents = document.querySelectorAll('[data-mw-revid]');
parents.forEach((e) => OCWaddLink(e));
function callback(mutationsList, observer) {
mutationsList.forEach((mutation) => {
if (mutation.type === 'childList') {
mutation.addedNodes.forEach((node) => {
if (node.nodeType === Node.ELEMENT_NODE && node.hasAttribute('data-mw-revid')) {
console.log('Element added:', node);
}
if (node.nodeType === Node.ELEMENT_NODE) {
const matchingDescendants = node.querySelectorAll('[data-mw-revid]');
matchingDescendants.forEach((descendant) => {
OCWaddLink(descendant)
});
}
});
}
});
}
if (mw.config.get("wgCanonicalSpecialPageName") == "Contributions") {
const mo = new MutationObserver(callback);
mo.observe(document.getElementsByClassName("mw-changeslist")[0], {
childList: true,
attributes: true,
subtree: true,
characterData: true
});
}
function OCWalternateAddLink() {
mw.util.addPortletLink('p-tb', '#', 'Welcome', 'OCWwelcome-wikibooks', 'Welcome this user');
mw.util.addPortletLink('p-tb', '#', 'Warn (level 1)', 'OCWwarn1-wikibooks', 'Warn this user');
mw.util.addPortletLink('p-tb', '#', 'Warn (level 2)', 'OCWwarn2-wikibooks', 'Warn this user');
mw.util.addPortletLink('p-tb', '#', 'Warn (level 3)', 'OCWwarn3-wikibooks', 'Warn this user');
$("#OCWwelcome-wikibooks").click(() => welcomeToWikibooks(mw.config.get("wgRelevantUserName")));
$("#OCWwarn1-wikibooks").click(() => warn(mw.config.get("wgRelevantUserName"), 1));
$("#OCWwarn2-wikibooks").click(() => warn(mw.config.get("wgRelevantUserName"), 2));
$("#OCWwarn3-wikibooks").click(() => warn(mw.config.get("wgRelevantUserName"), 3));
}
if(mw.config.get("wgCanonicalNamespace") == "User" || mw.config.get("wgCanonicalNamespace") == "User_talk" || mw.config.get("wgCanonicalSpecialPageName") == "Contributions") {
OCWalternateAddLink();
}
});
//</nowiki>