Difference between revisions of "MediaWiki:Common.js"
Greg.Fuller (talk | contribs) |
Greg.Fuller (talk | contribs) |
||
Line 10: | Line 10: | ||
function jfa_copy_to_clipboard (text) { | function jfa_copy_to_clipboard (text) { | ||
− | var old_height = $(document).height(); //store document height before modifications | + | //var old_height = $(document).height(); //store document height before modifications |
− | var old_scroll = $(window).scrollTop(); //remember the scroll position | + | //var old_scroll = $(window).scrollTop(); //remember the scroll position |
var dummy = document.createElement("textarea"); | var dummy = document.createElement("textarea"); | ||
+ | |||
+ | // prevent autoscroll? | ||
+ | document.getElementById("jfa-share").scrollTo(0, 1); | ||
+ | |||
+ | //document.body.prepend(dummy); | ||
document.body.appendChild(dummy); | document.body.appendChild(dummy); | ||
//document.getElementById("jfa-share").appendChild(dummy); | //document.getElementById("jfa-share").appendChild(dummy); | ||
+ | |||
+ | |||
dummy.value = text; | dummy.value = text; | ||
dummy.select(); | dummy.select(); | ||
Line 22: | Line 29: | ||
//document.getElementById("jfa-share").removeChild(dummy); | //document.getElementById("jfa-share").removeChild(dummy); | ||
− | $(document).scrollTop(old_scroll + $(document).height() - old_height); //restore "scroll position" | + | //$(document).scrollTop(old_scroll + $(document).height() - old_height); //restore "scroll position" |
} | } | ||
Line 68: | Line 75: | ||
//alert( "debugging: Handler for .click() called." ); | //alert( "debugging: Handler for .click() called." ); | ||
jfa_copy_to_clipboard(jfa_short_url); | jfa_copy_to_clipboard(jfa_short_url); | ||
− | alert("The short url for this page has been copied to your clipboard and is ready for sharing | + | alert("The short url for this page has been copied to your clipboard and is ready for sharing."); |
return false; | return false; | ||
Revision as of 11:04, 10 February 2019
/* Any JavaScript here will be loaded for all users on every page load. */
//Insert 'Justice for Animals .org' in the DOM, for the header jQuery(document).ready(function () {
document.body.insertAdjacentHTML( 'afterbegin', '
');
})
// copy to clipboard function //https://stackoverflow.com/questions/33855641/copy-output-of-a-javascript-variable-to-the-clipboard function jfa_copy_to_clipboard (text) {
//var old_height = $(document).height(); //store document height before modifications //var old_scroll = $(window).scrollTop(); //remember the scroll position
var dummy = document.createElement("textarea"); // prevent autoscroll? document.getElementById("jfa-share").scrollTo(0, 1); //document.body.prepend(dummy); document.body.appendChild(dummy); //document.getElementById("jfa-share").appendChild(dummy); dummy.value = text; dummy.select(); document.execCommand("copy"); document.body.removeChild(dummy); //document.getElementById("jfa-share").removeChild(dummy); //$(document).scrollTop(old_scroll + $(document).height() - old_height); //restore "scroll position"
}
// handle share template jQuery(document).ready(function () { if ( jQuery( "#jfa-share" ).length ) {
// twitter and facebook sharing // https://codepen.io/Huskie/pen/wKphk
// twitter sharing var twitterShare = document.querySelector('[data-js="twitter-share"]'); twitterShare.onclick = function(e) { e.preventDefault(); var twitterWindow = window.open('https://twitter.com/share?url=' + document.URL, 'twitter-popup', 'height=350,width=600'); if(twitterWindow.focus) { twitterWindow.focus(); } return false; }
// facebook sharing var facebookShare = document.querySelector('[data-js="facebook-share"]'); facebookShare.onclick = function(e) { e.preventDefault(); var facebookWindow = window.open('https://www.facebook.com/sharer/sharer.php?u=' + document.URL, 'facebook-popup', 'height=350,width=600'); if(facebookWindow.focus) { facebookWindow.focus(); } return false; }
// copy short url to clipboard
var jfa_short_url = jQuery("span.jfa-page-shorturl a").attr("href");
// don't know why list style not showing bullet on clipboard jQuery("#jfa-share ul").attr("list-style-type","bullet");
// set titles jQuery("#jfa-share a").attr("title","Click to share!");
// prevent bad hover tooltop jQuery(".jfa-share-option-url a").attr("href",jfa_short_url);
jQuery(".jfa-share-option-url a").click(function(event) { event.preventDefault(); //alert( "debugging: Handler for .click() called." ); jfa_copy_to_clipboard(jfa_short_url); alert("The short url for this page has been copied to your clipboard and is ready for sharing."); return false;
});
// if plain text exists, reveal second option and handle copy to clipboard if ( jQuery( "#jfa-plaintext" ).length ) { jQuery(".jfa-share-option-plaintext").css('display','inline');
// copy to clipboard on click jQuery(".jfa-share-option-plaintext a").click(function(event) { event.preventDefault(); //var jfa_plaintext = jQuery("#jfa-plaintext table tr:eq(1) td:eq(0)").html(); var jfa_plaintext = jQuery("#jfa-plaintext table tr:eq(1)").text();
// double up newlines to create blank line; then trim jfa_plaintext = jfa_plaintext.replace(new RegExp('\r?\n','g'), '\n\n'); jfa_plaintext = jQuery.trim(jfa_plaintext);
// copy to clipboard and inform jfa_copy_to_clipboard(jfa_plaintext); alert("The plain text version of this page has been copied to your clipboard and is ready for sharing."); }); } }
})