User:ClickerClock/scripts/QuickSign.js

From RationalWiki
Jump to navigation Jump to search

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 / Edge: 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.
//<nowiki>
//Shortcuts
$(document).ready(function() {
"use strict";
   // boilerplate
   function getSurroundings() {
      // Get the text cursor
      var cursor = jQuery("#wpTextbox1")[0].selectionStart;
      var cursorEnd = jQuery("#wpTextbox1")[0].selectionEnd;
      // Gets the first string
      var first = document.getElementById("wpTextbox1").value.substring(0, cursor);
      //Gets the second string
      var second = document.getElementById("wpTextbox1").value.substring(cursor);
      var a = {};
      a.cursor = cursor;
      a.cursorEnd = cursorEnd;
      a.first = first;
      a.second = second;
      return a;
   }

function insertString(insert, x) {
   if ('execCommand' in document) {
     document.execCommand("insertText", false, insert);
   }
}

function isTalk() {
   if (mw.config.get("wgCanonicalNamespace").slice(-4).toLowerCase() == "talk" || mw.config.get("wgCanonicalNamespace") == "Forum" || mw.config.get("wgCanonicalNamespace") == "Project" || mw.config.get("wgCanonicalNamespace") == "Debate" || mw.config.get("wgCanonicalNamespace") == "Thread") {
   return true;
   } else {
   return false;
   }
}

// Insert Sign name
   var insertSign = "—~~~";

      var i = isTalk()? addSign():null; //if false return null
      
function addSign() {
   $("body").on("keypress", "#wpTextbox1", function(e) {
      if (String.fromCharCode(e.which) === "~") {
         var x = getSurroundings();
         //Inserts variable between cursor
         insertString(insertSign, x);
         //set cursor back
         this.setSelectionRange(x.cursor + 5, x.cursorEnd + 4);
      }
   });
}

});
//</nowiki>