2017-08-29 43 views
1

免责声明:我是JS中的一个鸡巴,我真的不知道它,对不起,如果这真的是一个菜鸟问题。脚本替换网页中的一个词

我找到了一个允许你替换网页上的单词/短语的userscript。 Howevever,我有一条错误“预期的条件表达式,而是看到了任务”上线57

该生产线是:

for (i = 0; text = texts.snapshotItem(i); i += 1) { 

整个脚本:

(function() { 'use strict'; 
var words = { 
    'test 1':'test 2', 
    'bleh': 'blah' 
}; 

var regexs = [], 
    replacements = [], 
    tagsWhitelist = ['PRE', 'BLOCKQUOTE', 'CODE', 'INPUT', 'BUTTON', 'TEXTAREA'], 
    rIsRegexp = /^\/(.+)\/([gim]+)?$/, 
    word, text, texts, i, userRegexp; 

// prepareRegex by JoeSimmons 
// used to take a string and ready it for use in new RegExp() 
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]); 
     }); 
    } 
} 
}()); 

能有人向我解释这意味着什么(我喜欢理解我的所作所为),以及我如何解决这个问题?

回答

4

=是一个赋值操作符。

由于for循环需要一个条件,赋值在此上下文中无效。

您需要使用比较操作符如==<=>=

一个可能的解决方法是:

for (i = 0; text = texts.snapshotItem(i); i += 1) { 

for (i = 0; text == texts.snapshotItem(i); i += 1) { 
+0

完美,感谢您的快速答复和解释! – SBO

+0

没问题的队友。 – HumbleWebDev