Advertisement
Guest User

Sorcha Faal.user.js

a guest
Feb 25th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name           Sorcha Faal aka David Booth
  3. // @namespace      https://localhost
  4. // @description    replace "Sorcha Faal" on ALL websites with "David Booth"
  5. // @include        http://*
  6. // @include        https://*
  7. // @include        file://*
  8. // @copyright
  9. // @version        1.0.0
  10. // ==/UserScript==
  11. (function () {
  12.     'use strict';
  13.     var words = {
  14.     ///////////////////////////////////////////////////////
  15.  
  16.         // Syntax: 'Search word' : 'Replace word',
  17.         'Sorcha Faal' : 'David Booth',
  18.  
  19.  
  20.     ///////////////////////////////////////////////////////
  21.     '':''};
  22.  
  23.     var regexs = [], replacements = [],
  24.         tagsWhitelist = ['PRE', 'BLOCKQUOTE', 'CODE', 'INPUT', 'BUTTON', 'TEXTAREA'],
  25.         rIsRegexp = /^\/(.+)\/([gim]+)?$/,
  26.         word, text, texts, i, userRegexp;
  27.  
  28.     function prepareRegex(string) {
  29.         return string.replace(/([\[\]\^\&\$\.\(\)\?\/\\\+\{\}\|])/g, '\\$1');
  30.     }
  31.  
  32.     // function to decide whether a parent tag will have its text replaced or not
  33.     function isTagOk(tag) {
  34.         return tagsWhitelist.indexOf(tag) === -1;
  35.     }
  36.  
  37.     delete words['']; // so the user can add each entry ending with a comma,
  38.                       // I put an extra empty key/value pair in the object.
  39.                       // so we need to remove it before continuing
  40.  
  41.     // convert the 'words' JSON object to an Array
  42.     for (word in words) {
  43.         if ( typeof word === 'string' && words.hasOwnProperty(word) ) {
  44.             userRegexp = word.match(rIsRegexp);
  45.  
  46.             // add the search/needle/query
  47.             if (userRegexp) {
  48.                 regexs.push(
  49.                     new RegExp(userRegexp[1], 'g')
  50.                 );
  51.             } else {
  52.                 regexs.push(
  53.                     new RegExp(prepareRegex(word).replace(/\\?\*/g, function (fullMatch) {
  54.                         return fullMatch === '\\*' ? '*' : '[^ ]*';
  55.                     }), 'g')
  56.                 );
  57.             }
  58.  
  59.             // add the replacement
  60.             replacements.push( words[word] );
  61.         }
  62.     }
  63.  
  64.     // do the replacement
  65.     texts = document.evaluate('//body//text()[ normalize-space(.) != "" ]', document, null, 6, null);
  66.     for (i = 0; text = texts.snapshotItem(i); i += 1) {
  67.         if ( isTagOk(text.parentNode.tagName) ) {
  68.             regexs.forEach(function (value, index) {
  69.                 text.data = text.data.replace( value, replacements[index] );
  70.             });
  71.         }
  72.     }
  73.  
  74. }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement