User:Polygnotus/Scripts/WikiBlame2.js
Appearance
From Wikipedia, the free encyclopedia
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.
This code will be executed when previewing this page.
Documentation for this user script can be added at User:Polygnotus/Scripts/WikiBlame2.
// WikiBlame Tool for Wikipedia articles with configurable hotkey (function(){ // Only run on article pages (not special pages, talk pages, etc.) if(!mw.config.get('wgIsArticle')){ return; } // Default hotkey configuration constDEFAULT_HOTKEY={ ctrl:true, shift:true, alt:false, key:'L' }; // Load hotkey configuration from localStorage functionloadHotkeyConfig(){ try{ conststored=localStorage.getItem('wikiblame-hotkey'); returnstored?JSON.parse(stored):DEFAULT_HOTKEY; }catch(e){ returnDEFAULT_HOTKEY; } } // Save hotkey configuration to localStorage functionsaveHotkeyConfig(config){ try{ localStorage.setItem('wikiblame-hotkey',JSON.stringify(config)); }catch(e){ console.warn('Could not save WikiBlame hotkey configuration'); } } // Format hotkey for display functionformatHotkey(config){ constparts=[]; if(config.ctrl)parts.push('Ctrl'); if(config.shift)parts.push('Shift'); if(config.alt)parts.push('Alt'); parts.push(config.key.toUpperCase()); returnparts.join('+'); } // Check if pressed keys match the configured hotkey functionmatchesHotkey(event,config){ returnevent.ctrlKey===config.ctrl&& event.shiftKey===config.shift&& event.altKey===config.alt&& (event.key.toLowerCase()===config.key.toLowerCase()); } // Function to perform WikiBlame search functionperformWikiBlameSearch(){ // Get selected text constselectedText=window.getSelection().toString().trim(); if(!selectedText){ consthotkey=formatHotkey(loadHotkeyConfig()); alert(`Please select text first (then use ${hotkey} or the Tools menu)`); return; } // Get current language and article information from MediaWiki constlang=mw.config.get('wgContentLanguage'); constarticleTitle=mw.config.get('wgPageName'); // URL encode the selected text for the needle parameter constencodedNeedle=encodeURIComponent(selectedText); // Construct the WikiBlame URL constwikiblameUrl=`https://wikipedia.ramselehof.de/wikiblame.php?user_lang=${lang}&lang=${lang}&project=wikipedia&tld=org&article=${articleTitle}&needle=${encodedNeedle}`; // Open WikiBlame in a new tab/window window.open(wikiblameUrl,'_blank'); } // Show hotkey configuration popup functionshowHotkeyConfig(){ constcurrentConfig=loadHotkeyConfig(); // Remove any existing popup constexistingOverlay=document.getElementById('wikiblame-config-overlay'); if(existingOverlay){ existingOverlay.remove(); } // Create popup overlay constoverlay=document.createElement('div'); overlay.id='wikiblame-config-overlay'; overlay.style.cssText=` position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); z-index: 10000; display: flex; justify-content: center; align-items: center; `; // Create popup content constpopup=document.createElement('div'); popup.style.cssText=` background: white; padding: 20px; border-radius: 8px; box-shadow: 0 4px 12px rgba(0,0,0,0.3); max-width: 400px; width: 90%; font-family: sans-serif; `; popup.innerHTML=` <h3 style="margin-top: 0;">Configure WikiBlame Hotkey</h3> <p>Choose which key combination to use for WikiBlame search:</p> <div style="margin: 15px 0;"> <label style="display: block; margin: 8px 0;"> <input type="checkbox" id="wikiblame-ctrl" ${currentConfig.ctrl?'checked':''}> Ctrl </label> <label style="display: block; margin: 8px 0;"> <input type="checkbox" id="wikiblame-shift" ${currentConfig.shift?'checked':''}> Shift </label> <label style="display: block; margin: 8px 0;"> <input type="checkbox" id="wikiblame-alt" ${currentConfig.alt?'checked':''}> Alt </label> <label style="display: block; margin: 8px 0;"> Key: <input type="text" id="wikiblame-key" value="${currentConfig.key}" maxlength="1" style="width: 30px; text-align: center; margin-left: 10px;"> </label> </div> <div style="margin: 15px 0;"> <strong>Current hotkey:</strong> <span id="wikiblame-preview">${formatHotkey(currentConfig)}</span> </div> <div style="text-align: right; margin-top: 20px;"> <button id="wikiblame-cancel" style="margin-right: 10px; padding: 8px 16px;">Cancel</button> <button id="wikiblame-save" style="padding: 8px 16px; background: #0645ad; color: white; border: none; border-radius: 4px;">Save</button> </div> `; overlay.appendChild(popup); document.body.appendChild(overlay); // Update preview when inputs change functionupdatePreview(){ constconfig={ ctrl:document.getElementById('wikiblame-ctrl').checked, shift:document.getElementById('wikiblame-shift').checked, alt:document.getElementById('wikiblame-alt').checked, key:document.getElementById('wikiblame-key').value||'L' }; document.getElementById('wikiblame-preview').textContent=formatHotkey(config); } // Add event listeners for real-time preview document.getElementById('wikiblame-ctrl').addEventListener('change',updatePreview); document.getElementById('wikiblame-shift').addEventListener('change',updatePreview); document.getElementById('wikiblame-alt').addEventListener('change',updatePreview); document.getElementById('wikiblame-key').addEventListener('input',updatePreview); // Validate and format key input document.getElementById('wikiblame-key').addEventListener('input',function(e){ letvalue=e.target.value.toUpperCase(); if(value&&/^[A-Z0-9]$/.test(value)){ e.target.value=value; }elseif(value.length>0){ e.target.value=currentConfig.key; } updatePreview(); }); // Handle save button document.getElementById('wikiblame-save').addEventListener('click',function(){ constnewConfig={ ctrl:document.getElementById('wikiblame-ctrl').checked, shift:document.getElementById('wikiblame-shift').checked, alt:document.getElementById('wikiblame-alt').checked, key:document.getElementById('wikiblame-key').value||'L' }; // Validate that at least one modifier is selected if(!newConfig.ctrl&&!newConfig.shift&&!newConfig.alt){ alert('Please select at least one modifier key (Ctrl, Shift, or Alt)'); return; } saveHotkeyConfig(newConfig); overlay.remove(); // Update the tooltip of the existing WikiBlame link constwikiblameLink=document.getElementById('t-wikiblame'); if(wikiblameLink){ wikiblameLink.title=`Search for when selected text was added to this article (${formatHotkey(newConfig)})`; } }); // Handle cancel button and overlay click document.getElementById('wikiblame-cancel').addEventListener('click',function(){ overlay.remove(); }); overlay.addEventListener('click',function(e){ if(e.target===overlay){ overlay.remove(); } }); // Handle Escape key document.addEventListener('keydown',functionescapeHandler(e){ if(e.key==='Escape'){ overlay.remove(); document.removeEventListener('keydown',escapeHandler); } }); } letcurrentKeyboardHandler=null; // Set up keyboard handler with current config functionsetupKeyboardHandler(){ // Remove existing handler if(currentKeyboardHandler){ document.removeEventListener('keydown',currentKeyboardHandler); } constconfig=loadHotkeyConfig(); // Create new handler currentKeyboardHandler=function(e){ if(matchesHotkey(e,config)){ e.preventDefault(); performWikiBlameSearch(); } }; // Add new handler document.addEventListener('keydown',currentKeyboardHandler); } // Wait for the page to be fully loaded mw.loader.using(['mediawiki.util']).then(function(){ constcurrentConfig=loadHotkeyConfig(); // Add the WikiBlame link to the Tools menu mw.util.addPortletLink( 'p-tb', '#', 'WikiBlame search', 't-wikiblame', `Search for when selected text was added to this article (${formatHotkey(currentConfig)})` ); // Add the configuration link to the More menu mw.util.addPortletLink( 'p-cactions',// More menu ID (p-cactions is the "More" dropdown) '#', 'Configure WikiBlame hotkey', 't-wikiblame-config', 'Configure the keyboard shortcut for WikiBlame search' ); // Add click handler for the WikiBlame search document.getElementById('t-wikiblame').addEventListener('click',function(e){ e.preventDefault(); performWikiBlameSearch(); }); // Add click handler for the configuration document.getElementById('t-wikiblame-config').addEventListener('click',function(e){ e.preventDefault(); showHotkeyConfig(); }); // Set up the keyboard handler setupKeyboardHandler(); }); })();