User:Username/BlockAbuser.js: Difference between revisions
From Test Wiki
Content deleted Content added
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
mw.loader.using(['mediawiki.util', 'mediawiki.api', 'jquery'], function () { |
mw.loader.using(['mediawiki.util', 'mediawiki.api', 'jquery'], function () { |
||
// Run only on the exact Special:AbuseLog page (no subpages) |
|||
if (mw.config.get('wgPageName') !== 'Special:AbuseLog') { |
if (mw.config.get('wgPageName') !== 'Special:AbuseLog') { |
||
return; |
return; |
||
| Line 5: | Line 6: | ||
const $content = $('#mw-content-text'); |
const $content = $('#mw-content-text'); |
||
const userData = {}; // username |
const userData = {}; // username -> { latestLogId, extraHits, $userLink, $blockLink, allLinks } |
||
const ipRegex = /^(?:\d{1,3}\.){3}\d{1,3}$|:/; |
const ipRegex = /^(?:\d{1,3}\.){3}\d{1,3}$|:/; |
||
const api = new mw.Api(); |
|||
// Helper to decode username from a user link href |
|||
function getUsernameFromHref(href) { |
function getUsernameFromHref(href) { |
||
| ⚫ | |||
const |
const match = href.match(/\/wiki\/User:([^?#]+)/); |
||
| ⚫ | |||
| ⚫ | |||
return decodeURIComponent( |
return decodeURIComponent(match[1]).replace(/_/g, ' ').trim(); |
||
} |
} |
||
// 1 |
// Step 1: Collect user info from each abuse log entry (<li>) |
||
$content.find('li').each(function () { |
$content.find('li').each(function () { |
||
const $li = $(this); |
const $li = $(this); |
||
// Find all user links in this <li> |
|||
const $userLinks = $li.find('a').filter(function () { |
const $userLinks = $li.find('a').filter(function () { |
||
const href = $(this).attr('href') |
const href = $(this).attr('href'); |
||
return href.includes('/wiki/User:'); |
return href && href.includes('/wiki/User:'); |
||
}); |
}); |
||
if ($userLinks.length === 0) return; |
if ($userLinks.length === 0) return; |
||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
const href = $(this).attr('href'); |
|||
| ⚫ | |||
const match = href.match(/Special:AbuseLog\/(\d+)/); |
|||
| ⚫ | |||
| ⚫ | |||
return false; // stop iteration |
|||
} |
|||
}); |
|||
if (!logId) return; |
|||
// For each user link in this li: |
|||
$userLinks.each(function () { |
$userLinks.each(function () { |
||
const $ |
const $userLink = $(this); |
||
const username = getUsernameFromHref($ |
const username = getUsernameFromHref($userLink.attr('href')); |
||
if (!username || ipRegex.test(username)) return; |
|||
if (!username) return; |
|||
if (ipRegex.test(username)) return; // skip IP addresses |
|||
| ⚫ | |||
// Find the "block" link in this same <li> that applies to this user |
|||
// Block links usually have href with "block=" and user name |
|||
// Sometimes encoded spaces appear as + or %20 - check both |
|||
const encodedUser1 = encodeURIComponent(username).replace(/%20/g, '+'); |
|||
| ⚫ | |||
const encodedUser2 = encodeURIComponent(username).replace(/%20/g, '%20'); |
|||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
const $blockLink = $li.find('a').filter(function () { |
const $blockLink = $li.find('a').filter(function () { |
||
const href = $(this).attr('href') || ''; |
const href = $(this).attr('href') || ''; |
||
return href.includes('block=') && |
return href.includes('block=') && |
||
(href.includes(encodedUser1) || href.includes(encodedUser2)); |
|||
}).first(); |
}).first(); |
||
| Line 53: | Line 67: | ||
latestLogId: logId, |
latestLogId: logId, |
||
extraHits: 0, |
extraHits: 0, |
||
$ |
$userLink: $userLink, |
||
$userLink: $link, |
|||
$blockLink: $blockLink.length ? $blockLink : null, |
$blockLink: $blockLink.length ? $blockLink : null, |
||
allUserLinks: [$userLink] |
|||
}; |
}; |
||
} else { |
} else { |
||
userData[username]. |
userData[username].allUserLinks.push($userLink); |
||
if (parseInt(logId) > parseInt(userData[username].latestLogId)) { |
if (parseInt(logId, 10) > parseInt(userData[username].latestLogId, 10)) { |
||
userData[username].latestLogId = logId; |
userData[username].latestLogId = logId; |
||
userData[username].$ |
userData[username].$userLink = $userLink; |
||
userData[username].$userLink = $link; |
|||
if ($blockLink.length) userData[username].$blockLink = $blockLink; |
if ($blockLink.length) userData[username].$blockLink = $blockLink; |
||
} |
} |
||
| Line 71: | Line 83: | ||
}); |
}); |
||
// 2 |
// Step 2: Delink all but the most recent user link for each user |
||
Object.entries(userData).forEach(([username, data]) => { |
Object.entries(userData).forEach(([username, data]) => { |
||
data. |
data.allUserLinks.forEach(($link) => { |
||
if ($link[0] !== data.$userLink[0]) { |
if ($link[0] !== data.$userLink[0]) { |
||
$link.replaceWith(document.createTextNode($link.text())); |
$link.replaceWith(document.createTextNode($link.text())); |
||
| Line 80: | Line 92: | ||
}); |
}); |
||
// 3 |
// Step 3: Use MediaWiki API to check block status for all users found |
||
const usernames = Object.keys(userData); |
const usernames = Object.keys(userData); |
||
if (usernames.length === 0) { |
if (usernames.length === 0) { |
||
// No users found - just add the button and stop |
|||
addButtonAndSetup(userData); |
addButtonAndSetup(userData); |
||
return; |
return; |
||
| Line 98: | Line 111: | ||
data.query.users.forEach(user => { |
data.query.users.forEach(user => { |
||
if (user.blockid) { |
if (user.blockid) { |
||
const |
const uname = user.name; |
||
if (userData[ |
if (userData[uname] && userData[uname].$blockLink) { |
||
// Underline the block link to indicate |
// Underline and color the existing block link to indicate the user is blocked |
||
userData[ |
userData[uname].$blockLink.css({ |
||
'text-decoration': 'underline', |
'text-decoration': 'underline', |
||
'font-weight': 'bold', |
'font-weight': 'bold', |
||
| Line 111: | Line 124: | ||
}); |
}); |
||
} |
} |
||
addButtonAndSetup(userData); |
addButtonAndSetup(userData); |
||
}).fail(function () { |
}).fail(function () { |
||
| Line 117: | Line 129: | ||
}); |
}); |
||
// 4 |
// Step 4: Add the top button, open tabs & show summary on click |
||
function addButtonAndSetup(userData) { |
function addButtonAndSetup(userData) { |
||
const $btn = $('<button>') |
const $btn = $('<button>') |
||
| Line 142: | Line 154: | ||
const extraHits = parseInt(data.extraHits, 10); |
const extraHits = parseInt(data.extraHits, 10); |
||
// Open AbuseLog filtered |
// Open AbuseLog filtered for the user |
||
const abuseLogUrl = mw.util.getUrl('Special:AbuseLog', { |
const abuseLogUrl = mw.util.getUrl('Special:AbuseLog', { |
||
wpSearchUser: username |
wpSearchUser: username |
||
| Line 154: | Line 166: | ||
window.open(talkDeleteUrl, '_blank'); |
window.open(talkDeleteUrl, '_blank'); |
||
// |
// Build summary line |
||
let line = `[[Special:AbuseLog/${latestLogId}]]`; |
let line = `[[Special:AbuseLog/${latestLogId}]]`; |
||
if (extraHits > 0) { |
if (extraHits > 0) { |
||
| Line 162: | Line 174: | ||
}); |
}); |
||
alert( |
|||
'Spambot or spam-only accounts detected. Details:\n' + |
'Spambot or spam-only accounts detected. Details:\n' + |
||
summaryLines.join('\n') |
summaryLines.join('\n') |
||
| ⚫ | |||
| ⚫ | |||
}); |
}); |
||
} |
} |
||