Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Sorcha Faal aka David Booth
- // @namespace https://localhost
- // @description replace "Sorcha Faal" on ALL websites with "David Booth"
- // @include http://*
- // @include https://*
- // @include file://*
- // @copyright
- // @version 1.0.0
- // ==/UserScript==
- (function () {
- 'use strict';
- var words = {
- ///////////////////////////////////////////////////////
- // Syntax: 'Search word' : 'Replace word',
- 'Sorcha Faal' : 'David Booth',
- ///////////////////////////////////////////////////////
- '':''};
- var regexs = [], replacements = [],
- tagsWhitelist = ['PRE', 'BLOCKQUOTE', 'CODE', 'INPUT', 'BUTTON', 'TEXTAREA'],
- rIsRegexp = /^\/(.+)\/([gim]+)?$/,
- word, text, texts, i, userRegexp;
- function prepareRegex(string) {
- return string.replace(/([\[\]\^\&\$\.\(\)\?\/\\\+\{\}\|])/g, '\\$1');
- }
- // function to decide whether a parent tag will have its text replaced or not
- function isTagOk(tag) {
- return tagsWhitelist.indexOf(tag) === -1;
- }
- delete words['']; // so the user can add each entry ending with a comma,
- // I put an extra empty key/value pair in the object.
- // so we need to remove it before continuing
- // convert the 'words' JSON object to an Array
- for (word in words) {
- if ( typeof word === 'string' && words.hasOwnProperty(word) ) {
- userRegexp = word.match(rIsRegexp);
- // add the search/needle/query
- if (userRegexp) {
- regexs.push(
- new RegExp(userRegexp[1], 'g')
- );
- } else {
- regexs.push(
- new RegExp(prepareRegex(word).replace(/\\?\*/g, function (fullMatch) {
- return fullMatch === '\\*' ? '*' : '[^ ]*';
- }), 'g')
- );
- }
- // add the replacement
- replacements.push( words[word] );
- }
- }
- // do the replacement
- texts = document.evaluate('//body//text()[ normalize-space(.) != "" ]', document, null, 6, null);
- for (i = 0; text = texts.snapshotItem(i); i += 1) {
- if ( isTagOk(text.parentNode.tagName) ) {
- regexs.forEach(function (value, index) {
- text.data = text.data.replace( value, replacements[index] );
- });
- }
- }
- }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement