User:Username/BlockAbuser.js: Difference between revisions
From Test Wiki
Content deleted Content added
No edit summary |
No edit summary |
||
| Line 5: | Line 5: | ||
const $content = $('#mw-content-text'); |
const $content = $('#mw-content-text'); |
||
const userData = {}; // username => { latestLogId, extraHits, $li, $userLink, allLinks: [] } |
const userData = {}; // username => { latestLogId, extraHits, $li, $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(); |
const api = new mw.Api(); |
||
| Line 31: | Line 31: | ||
if (!username || ipRegex.test(username)) return; |
if (!username || ipRegex.test(username)) return; |
||
| ⚫ | |||
let logId = null; |
let logId = null; |
||
$li.find('a').each(function () { |
$li.find('a').each(function () { |
||
| Line 41: | Line 42: | ||
}); |
}); |
||
if (!logId) return; |
if (!logId) return; |
||
// Find block link for this user in this li (link with href containing 'block=' and username) |
|||
const $blockLink = $li.find('a').filter(function () { |
|||
| ⚫ | |||
return href.includes('block=') && href.includes(encodeURIComponent(username).replace(/%20/g, '+')); |
|||
| ⚫ | |||
if (!userData[username]) { |
if (!userData[username]) { |
||
| Line 48: | Line 55: | ||
$li: $li, |
$li: $li, |
||
$userLink: $link, |
$userLink: $link, |
||
$blockLink: $blockLink.length ? $blockLink : null, |
|||
allLinks: [$link] |
|||
}; |
}; |
||
} else { |
} else { |
||
| Line 57: | Line 64: | ||
userData[username].$li = $li; |
userData[username].$li = $li; |
||
userData[username].$userLink = $link; |
userData[username].$userLink = $link; |
||
if ($blockLink.length) userData[username].$blockLink = $blockLink; |
|||
} |
} |
||
userData[username].extraHits++; |
userData[username].extraHits++; |
||
| Line 72: | Line 80: | ||
}); |
}); |
||
// 3. Check blocked status via API |
// 3. Check blocked status via API |
||
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 return |
|||
addButtonAndSetup(userData); |
addButtonAndSetup(userData); |
||
return; |
return; |
||
} |
} |
||
const api = new mw.Api(); |
|||
// MediaWiki API query to check if users are blocked |
|||
api.get({ |
api.get({ |
||
action: 'query', |
action: 'query', |
||
| Line 91: | Line 99: | ||
if (user.blockid) { |
if (user.blockid) { |
||
const u = user.name; |
const u = user.name; |
||
if (userData[u]) { |
if (userData[u] && userData[u].$blockLink) { |
||
// Underline the block link to indicate this user is blocked |
|||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
'color': '#c00', |
|||
'cursor': 'pointer' |
|||
| ⚫ | |||
} |
} |
||
} |
} |
||
}); |
}); |
||
} |
} |
||
// Now update UI with "(blocked)" labels |
|||
Object.entries(userData).forEach(([username, data]) => { |
|||
if (data.blocked) { |
|||
// Append (blocked) after the user link (or text node) |
|||
| ⚫ | |||
// Check if (blocked) is already there to avoid duplicates |
|||
if (!$userLink.next('.blocked-label').length) { |
|||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
.css({ color: 'red', fontWeight: 'bold', marginLeft: '4px' }) |
|||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
}); |
|||
addButtonAndSetup(userData); |
addButtonAndSetup(userData); |
||
}).fail(function () { |
}).fail(function () { |
||
// If API fails, just add the button without blocked labels |
|||
addButtonAndSetup(userData); |
addButtonAndSetup(userData); |
||
}); |
}); |
||
// 4. Add control button and |
// 4. Add control button and setup click |
||
function addButtonAndSetup(userData) { |
function addButtonAndSetup(userData) { |
||
const $btn = $('<button>') |
const $btn = $('<button>') |
||
| Line 145: | Line 142: | ||
const extraHits = parseInt(data.extraHits, 10); |
const extraHits = parseInt(data.extraHits, 10); |
||
// Open filtered |
// Open AbuseLog filtered by user |
||
const abuseLogUrl = mw.util.getUrl('Special:AbuseLog', { |
const abuseLogUrl = mw.util.getUrl('Special:AbuseLog', { |
||
wpSearchUser: username |
wpSearchUser: username |
||
| Line 151: | Line 148: | ||
window.open(abuseLogUrl, '_blank'); |
window.open(abuseLogUrl, '_blank'); |
||
// Open User Talk deletion |
// Open User Talk deletion page with prefilled reason |
||
const talkDeleteUrl = mw.util.getUrl('Special:Delete/' + 'User_talk:' + encodeURIComponent(username), { |
const talkDeleteUrl = mw.util.getUrl('Special:Delete/' + 'User_talk:' + encodeURIComponent(username), { |
||
reason: `Talk page of an indefinitely blocked user that has little value. The content was: blahblah.` |
reason: `Talk page of an indefinitely blocked user that has little value. The content was: blahblah.` |
||