MediaWiki:Common.js: Difference between revisions
mNo edit summary |
mNo edit summary |
||
| Line 49: | Line 49: | ||
const content = notice.text().trim(); | const content = notice.text().trim(); | ||
// | // Generate a content-based hash | ||
const hash = generateHash(content); | const hash = generateHash(content); | ||
const key = `dismissed-notice-${pageTitle}-${hash}-${i}`; | const key = `dismissed-notice-${pageTitle}-${hash}-${i}`; | ||
| Line 65: | Line 65: | ||
// Add Reset Notices tab to action menu | // Add Reset Notices tab to action menu | ||
mw.util.addPortletLink( | mw.util.addPortletLink( | ||
'p-cactions', // Add to "Actions" menu | 'p-cactions', // Add to the "Actions" menu | ||
'#', | '#', | ||
'Reset Notices', | 'Reset Notices', | ||
'ca-reset-notices', | 'ca-reset-notices', | ||
'Reset all dismissed notices on this page', | 'Reset all dismissed notices on this page', | ||
null, | null, | ||
'#ca-history' | '#ca-history' | ||
); | ); | ||
// | // Reset logic | ||
$('#ca-reset-notices a').on('click', function (e) { | $('#ca-reset-notices a').on('click', function (e) { | ||
e.preventDefault(); | e.preventDefault(); | ||
const prefix = `dismissed-notice-${pageTitle}-`; | |||
const keysToRemove = []; | |||
for (let i = 0; i < localStorage.length; i++) { | for (let i = 0; i < localStorage.length; i++) { | ||
const key = localStorage.key(i); | const key = localStorage.key(i); | ||
if (key && key.startsWith( | if (key && key.startsWith(prefix)) { | ||
keysToRemove.push(key); | |||
} | } | ||
} | } | ||
// Reload | // Remove all matching keys | ||
keysToRemove.forEach(key => localStorage.removeItem(key)); | |||
// Reload to re-show all notices | |||
location.reload(); | location.reload(); | ||
}); | }); | ||
// Basic hash function | |||
function generateHash(str) { | function generateHash(str) { | ||
let hash = 0; | let hash = 0; | ||
for (let i = 0; i < str.length; i++) { | for (let i = 0; i < str.length; i++) { | ||
hash = ((hash << 5) - hash) + str.charCodeAt(i); | hash = ((hash << 5) - hash) + str.charCodeAt(i); | ||
hash |= 0; | hash |= 0; // Convert to 32bit integer | ||
} | } | ||
return hash; | return hash; | ||
} | } | ||
}); | }); | ||