Jump to content

User:Polygnotus/Scripts/ArchiveSearch.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// ==UserScript==
// @name         User Archive Search
// @namespace    http://en.wikipedia.org/wiki/User:YourUsername
// @version      1.4
// @description  Adds a link to search user and talk page archives for current user
// @match        https://en.wikipedia.org/wiki/User_talk:*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    
    // Check if we're on a user talk page
    if (mw.config.get('wgNamespaceNumber') !== 3) {
        return; // Exit if not on a user talk page
    }
    
    // Get the current user's username
    var currentUser = mw.config.get('wgUserName');
    
    // Exit if user is not logged in
    if (!currentUser) {
        return;
    }
    
    // Get the username from the user talk page
    var pageName = mw.config.get('wgPageName');
    var talkPageOwner = pageName.replace('User_talk:', '');
    
    // Decode the username in case it contains special characters
    talkPageOwner = decodeURIComponent(talkPageOwner);
    
    // Encode both usernames for the URL
    var encodedCurrentUser = encodeURIComponent(currentUser);
    var encodedTalkPageOwner = encodeURIComponent(talkPageOwner);
    
    // Construct the search URL that searches both namespaces
    // Using intitle: to search for pages with the username in the title
    var searchQuery = encodedCurrentUser + ' intitle:"' + talkPageOwner + '/"';
    var searchUrl = 'https://en.wikipedia.org/w/index.php?search=' + searchQuery + 
                    '&title=Special%3ASearch&profile=advanced&fulltext=1&ns2=1&ns3=1';
    
    // Add the portlet link
    $(document).ready(function() {
        mw.util.addPortletLink(
            'p-cactions',                // Add to the "More" dropdown menu
            searchUrl,                   // URL
            'Search archives',           // Link text
            'ca-search-archives',        // Link ID
            'Search ' + talkPageOwner + '\'s archives for mentions of ' + currentUser,  // Tooltip
            null,                        // Access key (null for none)
            '#ca-history'               // Insert before the history tab
        );
    });
})();