MediaWiki:Common.js

From Elanthipedia
Revision as of 02:10, 29 December 2023 by SIMU-COREY (talk | contribs) (Added wgTitle assignment as requested via TechHelp email)
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
// Select all text inside of the element(s) calling this.
jQuery.fn.selectText = function(){
    var doc = document;
    var element = this[0];
    //console.log(this, element);
    if (doc.body.createTextRange) {
        var range = document.body.createTextRange();
        range.moveToElementText(element);
        range.select();
    } else if (window.getSelection) {
        var selection = window.getSelection();        
        var range = document.createRange();
        range.selectNodeContents(element);
        selection.removeAllRanges();
        selection.addRange(range);
    }
};

// Add a Select All link before any pre tags on (script) pages or pre tags with a select class.
$(function () {
    $("pre").each(function() { 
        var pre = this;
        let wgTitle = mw.config.get('wgTitle');
	    if ($(pre).hasClass("select") || wgTitle.includes("(script)"))
        {
            var link = $("<a href='#'>(Select All)</a>").on("click", function(event) { 
                $(pre).selectText();
                event.preventDefault(); 
            }); 
            link.insertBefore(this);}
        }
    );
});