|
4 | 4 | // license that can be found in the LICENSE file or at |
5 | 5 | // https://developers.google.com/open-source/licenses/bsd |
6 | 6 |
|
7 | | -// Replace text on the page using a static list of patterns |
8 | | -function textReplacer(replacements) { |
9 | | - const replacementPatterns = buildReplacementRegex(replacements); |
10 | | - replaceText(replacementPatterns); |
11 | | -} |
12 | | - |
13 | | -function buildReplacementRegex(source) { |
14 | | - const output = []; |
15 | | - for (var i = 0; i < source.length; i++) { |
16 | | - if (!source[i]) { continue; } |
17 | | - const [find, replace] = source[i]; |
18 | | - const sanitizedMatch = escapeRegExp(find); |
19 | | - const findExp = new RegExp(`\\b${sanitizedMatch}\\b`, 'gi'); |
20 | | - output[i] = [findExp, replace]; |
| 7 | +// Wrap the in an immediately invoked function expression (IIFE) in order to |
| 8 | +// prevent local variables from polluting global scope |
| 9 | +(function () { |
| 10 | + const GET_REPLACEMENTS_MESSAGE_ID = 'get-replacements'; |
| 11 | + |
| 12 | + |
| 13 | + function buildReplacementRegex(source) { |
| 14 | + const output = []; |
| 15 | + for (let i = 0; i < source.length; i++) { |
| 16 | + if (!source[i]) { continue; } |
| 17 | + const [find, replace] = source[i]; |
| 18 | + const sanitizedMatch = escapeRegExp(find); |
| 19 | + const findExp = new RegExp(`\\b${sanitizedMatch}\\b`, 'gi'); |
| 20 | + output[i] = [findExp, replace]; |
| 21 | + } |
| 22 | + return output; |
| 23 | + } |
| 24 | + |
| 25 | + // This may not cover all special characters used in regular repressions. For |
| 26 | + // example purposes only. |
| 27 | + var REGEXP_SPECIAL_CHARACTERS = /[.(){}^$*+?[\]\\]/g; |
| 28 | + /** Sanitize user input to prevent unexpected behavior during RegExp execution */ |
| 29 | + function escapeRegExp(pattern) { |
| 30 | + return pattern.replace(REGEXP_SPECIAL_CHARACTERS, "\\$&") |
21 | 31 | } |
22 | | - return output; |
23 | | -} |
24 | | - |
25 | | -// Use var to avoid "Identifier 'REGEXP_SPECIAL_CHARACTERS' has already been |
26 | | -// declared" errors when running multiple times on the same page. |
27 | | -var REGEXP_SPECIAL_CHARACTERS = /[.(){}^$*+?[\]\\]/g; |
28 | | -/** Sanitize user input to prevent unexpected behavior during RegExp execution */ |
29 | | -function escapeRegExp(pattern) { |
30 | | - return pattern.replace(REGEXP_SPECIAL_CHARACTERS, "\\$&") |
31 | | -} |
32 | | - |
33 | | -/** Iterate through all text nodes and replace */ |
34 | | -function replaceText(replacements) { |
35 | | - let node; |
36 | | - const nodeIterator = document.createNodeIterator(document.body, NodeFilter.SHOW_TEXT); |
37 | | - while (node = nodeIterator.nextNode()) { |
38 | | - for (let [find, replace] of replacements) { |
39 | | - node.nodeValue = node.nodeValue.replace(find, replace); |
| 32 | + |
| 33 | + /** Iterate through all text nodes and replace */ |
| 34 | + function replaceText(replacements) { |
| 35 | + const nodeIterator = document.createNodeIterator(document.body, NodeFilter.SHOW_TEXT); |
| 36 | + |
| 37 | + let node; |
| 38 | + while (node = nodeIterator.nextNode()) { |
| 39 | + for (let [find, replace] of replacements) { |
| 40 | + node.nodeValue = node.nodeValue.replace(find, replace); |
| 41 | + } |
40 | 42 | } |
41 | 43 | } |
42 | | -} |
43 | 44 |
|
44 | | -// Replace text on the page using a list of patterns loaded from storage |
45 | | -chrome.storage.sync.get(['patterns'], function(data) { |
46 | | - textReplacer(data.patterns); |
47 | | -}); |
| 45 | + // Get the patterns to replace from storage, then build a regex from them and |
| 46 | + // replace all text on the page. |
| 47 | + chrome.runtime.sendMessage({ id: GET_REPLACEMENTS_MESSAGE_ID }) |
| 48 | + .then(function(data) { |
| 49 | + const replacementPatterns = buildReplacementRegex(data.patterns); |
| 50 | + replaceText(replacementPatterns); |
| 51 | + }); |
| 52 | + |
| 53 | +})() |
0 commit comments