User:Max/TBanHelper.js
Appearance
< User:Max
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.
This code will be executed when previewing this page.
This user script seems to have a documentation page at User:Max/TBanHelper.
//<nowiki>
mw.loader.using(['mediawiki.api', 'mediawiki.util', 'mediawiki.widgets', 'oojs-ui'], function () {
'use strict';
const page = mw.config.get('wgPageName');
const ns = mw.config.get('wgNamespaceNumber').toString();
const user = mw.config.get('wgUserName');
const prefsPage = `User:${user}/TBanPreferences.json`;
const prefs = {
banned: [],
namespaces: []
};
let api = new mw.Api();
let token = null;
async function loadPrefs() {
try {
const res = await api.get({
action: 'query',
format: 'json',
prop: 'revisions',
titles: prefsPage,
rvprop: 'content',
origin: '*'
});
const pages = res.query.pages;
const data = pages[Object.keys(pages)[0]];
if (data.revisions && data.revisions[0]) {
Object.assign(prefs, JSON.parse(data.revisions[0]['*']));
}
} catch (e) {
console.error('Error loading prefs:', e);
}
}
async function getToken() {
try {
const res = await api.get({
action: 'query',
meta: 'tokens',
type: 'csrf'
});
token = res.query.tokens.csrftoken;
} catch (e) {
console.error('Error getting token:', e);
throw e;
}
}
async function savePrefs() {
try {
if (!token) await getToken();
const content = JSON.stringify(prefs);
const res = await api.postWithToken('csrf', {
action: 'edit',
title: prefsPage,
summary: 'Updating preferences ([[User:DreamRimmer/TBanHelper|TBanHelper.js]])',
text: content,
format: 'json'
});
if (res.edit && res.edit.result === 'Success') {
alert('Preferences saved!');
location.reload();
} else {
throw new Error('Error saving prefs');
}
} catch (e) {
console.error('Error saving prefs:', e);
alert('Error saving prefs: ' + e.message);
}
}
class TBanPreferencesDialog extends OO.ui.ProcessDialog {
constructor(config) {
super(config);
}
static get static() {
return {
name: 'tbanPreferencesDialog',
title: 'TBan Preferences',
actions: [
{ action: 'save', label: 'Save', flags: ['primary', 'progressive'] },
{ action: 'cancel', label: 'Cancel', flags: ['safe', 'close'] }
]
};
}
initialize() {
super.initialize();
this.content = new OO.ui.PanelLayout({
padded: true,
expanded: false
});
this.bannedInput = new OO.ui.MultilineTextInputWidget({
value: prefs.banned.join(', '),
multiline: true,
rows: 4,
placeholder: 'Enter words, separated by commas'
});
this.namespacesInput = new OO.ui.TextInputWidget({
value: prefs.namespaces.join(', '),
multiline: true,
rows: 3,
placeholder: 'Enter namespace codes (comma separated, leave blank to apply to all)'
});
this.fieldset = new OO.ui.FieldsetLayout({
label: 'Preferences'
});
this.fieldset.addItems([
new OO.ui.FieldLayout(this.bannedInput, { label: 'Words:', align: 'top' }),
new OO.ui.FieldLayout(this.namespacesInput, { label: 'Namespaces:', align: 'top' })
]);
this.content.$element.append(this.fieldset.$element);
this.$body.append(this.content.$element);
}
getActionProcess(action) {
if (action === 'save') {
return new OO.ui.Process(() => {
prefs.banned = this.bannedInput.getValue().split(',').map(w => w.trim()).filter(w => w);
prefs.namespaces = this.namespacesInput.getValue().split(',').map(n => n.trim()).filter(n => n);
savePrefs();
this.close();
});
} else if (action === 'cancel') {
return new OO.ui.Process(() => {
this.close();
});
}
return super.getActionProcess(action);
}
}
async function fetchText() {
const res = await api.get({
action: 'query',
format: 'json',
prop: 'revisions',
titles: page,
rvprop: 'content',
origin: '*'
});
const pages = res.query.pages;
const data = pages[Object.keys(pages)[0]];
return data.revisions && data.revisions[0] ? data.revisions[0]['*'] : null;
}
function showBanner(matches) {
const div = document.createElement("div");
div.style.backgroundColor = "red";
div.style.color = "white";
div.style.padding = "10px";
div.style.textAlign = "center";
div.style.fontSize = "14px";
div.style.fontWeight = "bold";
div.style.position = "relative";
div.innerHTML = `Caution: This page may be part of your topic ban (matched: ${matches.map(word => `<span style="color: yellow;">${word}</span>`).join(', ')})`;
const btn = document.createElement("button");
btn.textContent = "x";
btn.style.position = "absolute";
btn.style.top = "2px";
btn.style.right = "5px";
btn.style.border = "none";
btn.style.padding = "1px 3px";
btn.style.cursor = "pointer";
btn.style.borderRadius = "3px";
btn.onclick = () => div.style.display = "none";
div.appendChild(btn);
$('#contentSub').before(div);
}
async function checkWords() {
const text = await fetchText();
if (text) {
const matches = prefs.banned.filter(word => new RegExp(`\\b${word}\\b`, 'i').test(text));
if (matches.length > 0) showBanner(matches);
}
}
async function init() {
await loadPrefs();
mw.util.addPortletLink(
'p-cactions',
'#',
'TBan Preferences',
'ca-tban-prefs',
'Manage topic ban preferences'
);
document.getElementById('ca-tban-prefs').addEventListener('click', function (e) {
e.preventDefault();
const windowManager = new OO.ui.WindowManager();
$(document.body).append(windowManager.$element);
const tbanPreferencesDialog = new TBanPreferencesDialog();
windowManager.addWindows([tbanPreferencesDialog]);
windowManager.openWindow(tbanPreferencesDialog);
});
if (prefs.namespaces.length === 0 || prefs.namespaces.includes(ns)) {
await checkWords();
}
}
init();
});
//</nowiki>