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();


     // Construct unique key per notice per page, using index + content hash
     // 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 (use 'p-personal' or 'p-navigation' if preferred)
     'p-cactions', // Add to the "Actions" menu
     '#',         // Link href (will be intercepted)
     '#',
     'Reset Notices', // Link text
     'Reset Notices',
     'ca-reset-notices', // ID
     'ca-reset-notices',
     'Reset all dismissed notices on this page', // Tooltip
     'Reset all dismissed notices on this page',
     null,
     null,
     '#ca-history' // Position before the history tab
     '#ca-history'
   );
   );


   // Handle reset click
   // Reset logic
   $('#ca-reset-notices a').on('click', function (e) {
   $('#ca-reset-notices a').on('click', function (e) {
     e.preventDefault();
     e.preventDefault();


     // Clear only dismissed notices related to this page
     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(`dismissed-notice-${pageTitle}-`)) {
       if (key && key.startsWith(prefix)) {
         localStorage.removeItem(key);
         keysToRemove.push(key);
       }
       }
     }
     }


     // Reload the page to re-show notices
    // 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;
   }
   }
});
});