Difference between revisions of "MediaWiki:Common.js"

Jump to: navigation, search

Difference between revisions of "MediaWiki:Common.js"

 
(131 intermediate revisions by 2 users not shown)
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
+
// Terms Modal
// By User:Pile0nades.
+
$(document).ready(function() {
// Makes the diff page display as a single column, instead of two.
 
$j(function() {
 
addOnloadHook(function() {
 
    // ==UserScript==
 
    // @name          Wikipedia single column diffs
 
    // @namespace      http://mywebsite.com/myscripts
 
    // @description    Make Wikipedia diff pages read like patch-format diffs, only one column needed
 
    // @include        http://en.wikipedia.org/w/index.php?*&diff=*
 
    // ==/UserScript==
 
  
 +
  setTimeout(function() {
 +
    const termsModal = $('#termsModal');
  
 +
    if (termsModal.length) {
 +
      // Check if the "rpmTermsAgreement" cookie is present
 +
      if (!document.cookie.includes('rpmTermsAgreement')) {
 +
        // Show the Bootstrap modal
 +
        termsModal.modal({
 +
          backdrop: "static"
 +
        });
 +
      }
  
    // remove the header for the previous revision, and add the next/prev links to the second header
+
      // Agree button click event to set the "rpm" cookie
    var prevHeader = get("//table[@class='diff'][1]/tbody/tr[1]/td[@class='diff-otitle']");
+
      $('#agreeTermsButton').click(function() {
    var nextHeader = get("//table[@class='diff'][1]/tbody/tr[1]/td[@class='diff-ntitle']");
+
        termsModal.modal('hide');
    var oldedit = get("//table[@class='diff'][1]/tbody/tr[1]/td[@class='diff-otitle']/a[@id='differences-prevlink']");
+
       
    var newedit = get("//table[@class='diff'][1]/tbody/tr[1]/td[@class='diff-ntitle']/a[@id='differences-nextlink']");
+
        // Set the "rpmTermsAgreement" cookie with expiration date five years from now
    for (var i = 0; i < prevHeader.snapshotLength; i++) {
+
        var expirationDate = new Date();
         if (oldedit.snapshotLength != 0) {
+
         expirationDate.setFullYear(expirationDate.getFullYear() + 5);
            nextHeader.snapshotItem(i).appendChild(oldedit.snapshotItem(0));
+
         document.cookie = "rpmTermsAgreement=true; expires=" + expirationDate.toUTCString() + "; path=/";
         }
+
      });
        if (oldedit.snapshotLength != 0 && newedit.snapshotLength != 0) {
 
            nextHeader.snapshotItem(i).appendChild(document.createTextNode(" | "));
 
        }
 
        if (newedit.snapshotLength != 0) {
 
            nextHeader.snapshotItem(i).appendChild(newedit.snapshotItem(0));
 
        }
 
        remove(prevHeader.snapshotItem(i));
 
 
     }
 
     }
 +
  }, 200);
  
 +
  /* MailChimp Form */
  
 +
  var mailchimpFormHTML =
 +
    '<div id="mc_embed_signup">' +
 +
    '    <form action="https://rcabc.us21.list-manage.com/subscribe/post?u=aa570610c198792349997a983&amp;id=91bc758592&amp;f_id=000857e1f0" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_self">' +
 +
    '        <div id="mc_embed_signup_scroll">' +
 +
    '            <div class="mc-field-group form-group">' +
 +
    '                <label for="mce-EMAIL">Email Address</label>' +
 +
    '                <input type="email" value="" name="EMAIL" class="required email form-control" id="mce-EMAIL" required>' +
 +
    '                <span id="mce-EMAIL-HELPERTEXT" class="helper_text"></span>' +
 +
    '            </div>' +
 +
    '            <div id="mce-responses" class="clear foot">' +
 +
    '                <div class="response" id="mce-error-response" style="display:none"></div>' +
 +
    '                <div class="response" id="mce-success-response" style="display:none"></div>' +
 +
    '            </div>' +
 +
    '            <div style="position: absolute; left: -5000px;" aria-hidden="true"><input type="text" name="b_aa570610c198792349997a983_91bc758592" tabindex="-1" value=""></div>' +
 +
    '            <input type="submit" value="Subscribe To RPM Updates" name="subscribe" id="mc-embedded-subscribe" class="btn btn-primary">' +
 +
    '        </div>' +
 +
    '    </form>' +
 +
    '</div>';
  
    // clone rows where the line changed but wasn't added/deleted, and delete where appropriate
+
  $('#mailchimpForm').html(mailchimpFormHTML);
    var changedLines = get("//table[@class='diff'][1]/tbody/tr/td[@class='diff-addedline' and position()=4]/../td[@class='diff-deletedline' and position()=2]")
+
});
    for (var i = 0; i < changedLines.snapshotLength; i++) {
 
        var cell = changedLines.snapshotItem(i);
 
 
 
        cell.parentNode.parentNode.insertBefore(
 
            cell.parentNode.cloneNode(true),
 
            cell.parentNode
 
        );
 
 
 
        remove(cell.previousSibling.previousSibling);
 
        remove(cell);
 
    }
 
 
 
 
 
 
 
    // delete blank cells in the first column to move the added lines into the first column
 
    var blankcells = get("//table[@class='diff'][1]/tbody/tr/td[1]");
 
    for (var i = 0; i < blankcells.snapshotLength; i++) {
 
        if (blankcells.snapshotItem(i).innerHTML == " ") {
 
            remove(blankcells.snapshotItem(i));
 
        }
 
    }
 
 
 
    // delete everything past the second td in each column
 
    var waste = get("//table[@class='diff'][1]/tbody/tr/td[2]");
 
    for (var i = 0; i < waste.snapshotLength; i++) {
 
        while (waste.snapshotItem(i).nextSibling) {
 
            remove(waste.snapshotItem(i).nextSibling);
 
        }
 
    }
 
 
 
    // delete the 2nd line number cell in each column
 
    var line = get("//table[@class='diff'][1]/tbody/tr/td[@class='diff-lineno'][1]");
 
    for (var i = 0; i < line.snapshotLength; i++) {
 
        remove(line.snapshotItem(i));
 
    }
 
 
 
    // delete the extra cols
 
    var cols = get("//table[@class='diff'][1]/col");
 
    for (var i = 0; i < cols.snapshotLength; i++) {
 
        var col = cols.snapshotItem(i);
 
        if (col.className == "diff-content") {
 
            col.style.width = "98%";
 
            col.removeAttribute("class");
 
        }
 
        if (i > 1) remove(col);
 
    }
 
 
 
    // shorthand function to remove nodes
 
    function remove(node) {
 
        node.parentNode.removeChild(node);
 
    }
 
 
 
 
 
    // xpath function
 
    function get(query) {
 
        return document.evaluate(
 
            query,
 
            document,
 
            null,
 
            XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
 
            null
 
        );
 
    }
 
})});
 
//
 

Latest revision as of 22:13, 19 June 2023

// Terms Modal
$(document).ready(function() {

  setTimeout(function() {
    const termsModal = $('#termsModal');

    if (termsModal.length) {
      // Check if the "rpmTermsAgreement" cookie is present
      if (!document.cookie.includes('rpmTermsAgreement')) {
        // Show the Bootstrap modal
        termsModal.modal({
          backdrop: "static"
        });
      }

      // Agree button click event to set the "rpm" cookie
      $('#agreeTermsButton').click(function() {
        termsModal.modal('hide');
        
        // Set the "rpmTermsAgreement" cookie with expiration date five years from now
        var expirationDate = new Date();
        expirationDate.setFullYear(expirationDate.getFullYear() + 5);
        document.cookie = "rpmTermsAgreement=true; expires=" + expirationDate.toUTCString() + "; path=/";
      });
    }
  }, 200);

  /* MailChimp Form */

  var mailchimpFormHTML =
    '<div id="mc_embed_signup">' +
    '    <form action="https://rcabc.us21.list-manage.com/subscribe/post?u=aa570610c198792349997a983&amp;id=91bc758592&amp;f_id=000857e1f0" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_self">' +
    '        <div id="mc_embed_signup_scroll">' +
    '            <div class="mc-field-group form-group">' +
    '                <label for="mce-EMAIL">Email Address</label>' +
    '                <input type="email" value="" name="EMAIL" class="required email form-control" id="mce-EMAIL" required>' +
    '                <span id="mce-EMAIL-HELPERTEXT" class="helper_text"></span>' +
    '            </div>' +
    '            <div id="mce-responses" class="clear foot">' +
    '                <div class="response" id="mce-error-response" style="display:none"></div>' +
    '                <div class="response" id="mce-success-response" style="display:none"></div>' +
    '            </div>' +
    '            <div style="position: absolute; left: -5000px;" aria-hidden="true"><input type="text" name="b_aa570610c198792349997a983_91bc758592" tabindex="-1" value=""></div>' +
    '            <input type="submit" value="Subscribe To RPM Updates" name="subscribe" id="mc-embedded-subscribe" class="btn btn-primary">' +
    '        </div>' +
    '    </form>' +
    '</div>';

  $('#mailchimpForm').html(mailchimpFormHTML);
});