MediaWiki:Common.js: Difference between revisions

From Elanthipedia
Jump to navigation Jump to search
(Adding Select All button to scripts)
No edit summary
Line 1: Line 1:
// Select all text inside of the element(s) calling this.
jQuery.fn.selectText = function(){
jQuery.fn.selectText = function(){
var doc = document;
var doc = document;
Line 16: Line 17:
};
};


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

Revision as of 23:44, 28 April 2020

// 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;
	    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);}
        }
    );
});