User:Polygnotus/Scripts/ArchiveSearch.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | Documentation for this user script can be added at User:Polygnotus/Scripts/ArchiveSearch. |
// ==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
);
});
})();