To get updates on new site content, like us on Facebook or follow us on Twitter.

MediaWiki:Common.js

From JFA Wiki
Revision as of 21:45, 17 February 2019 by Greg.Fuller (talk | contribs) (Redo generate plaintext for fact sheet, using textarea)

Note: After saving, 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: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.

/* Any JavaScript here will be loaded for all users on every page load. */

var iOS = /iPad|iPhone|iPod/.test(navigator.userAgent) && !window.MSStream;


/* this was moved to jfa.css in the Mediawiki installation directory because of a bug in the Refresh skin not loading common.js and common.css. Sucks Insert 'Justice for Animals .org' in the DOM, for the vector and refreshed skin only jQuery(document).ready(function () {

document.body.insertAdjacentHTML( 'afterbegin', '

<a href="/Main_Page">Justice for Animals .org</a>

');

})

  • /

// copy to clipboard function. //https://stackoverflow.com/questions/33855641/copy-output-of-a-javascript-variable-to-the-clipboard function jfa_copy_to_clipboard (text) {

// https://stackoverflow.com/questions/5688362/how-to-prevent-scrolling-on-prepend

   var dummy = document.createElement("textarea");
   

if (iOS) {

   	window.scrollTo(0, jQuery("#jfa-share").offset().top);

}

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

}


// autogenerate plaintext for factsheet (this needs to come before "handle share template") jQuery(document).ready(function () {

 if ( ! jQuery( "#jfa-plaintext" ).hasClass( "jfa-plaintext-factsheet" )) {
 	return;
 }
 	var jfa_maintext = jQuery("#jfa-maintext").text();
 	
 	// handle text in template (top text)
 	jfa_toptext = jQuery(".jfa-plaintext-factsheet table tr:eq(2) td").text();
 	jfa_toptext = jQuery.trim(jfa_toptext);

jfa_maintext = "

" + jfa_toptext + jfa_maintext; // put in a beginning paragraph //jfa_maintext = '

' + jfa_maintext; // trim early, trim often jfa_maintext = jQuery.trim(jfa_maintext); // remove edit buttons jfa_maintext = jfa_maintext.replace(/\[edit\]/g, ); jfa_maintext = jfa_maintext.replace(/\[edit source\]/g, ); jfa_maintext = jfa_maintext.replace(/\[edit | edit source\]/g, ); // fix headings jfa_maintext = jfa_maintext.replace(/\nFacts/g, ); jfa_maintext = jfa_maintext.replace(/\nContext/g, '\nCONTEXT'); jfa_maintext = jfa_maintext.replace(/\nPrimary Sources/g, '\nPRIMARY SOURCES'); jfa_maintext = jfa_maintext.replace(/\nSecondary Sources/g, '\nSECONDARY SOURCES'); jfa_maintext = jfa_maintext.replace(/\nSources/g, '\nSOURCES'); jfa_maintext = jfa_maintext.replace(/\n/g, "

\n"); // stick it in jQuery(".jfa-plaintext-factsheet table tr:eq(2) td").html('<textarea>' + jfa_maintext + '</textarea>'); }) // 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'); // put in plaintext in a textarea so it can be copied manually var jfa_plaintext = jQuery("#jfa-plaintext table tr:eq(2)").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); jQuery("#jfa-plaintext table tr:eq(2)").html('<textarea>' + jfa_plaintext + '</textarea>'); // copy to clipboard on click jQuery(".jfa-share-option-plaintext a").click(function(event) { event.preventDefault(); var jfa_clipboardtext = jQuery("#jfa-plaintext table tr:eq(2) textarea").text(); jfa_copy_to_clipboard(jfa_clipboardtext); alert("The plain text version of this page has been copied to your clipboard and is ready for sharing."); }); } } })