免责声明:我是JS的鸡巴,我真的不知道,对不起,如果这确实是个菜鸟问题。
我找到了一个允许您替换网页上的单词/短语的用户标记。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]);
} );
}
}
} () );
有人可以向我解释它的含义(我想了解我的工作),以及如何解决这个问题?
=
是一个分配运算符。
在这种情况下,分配是无效的,因为for循环期望条件。
您需要使用比较操作员,例如==
,<=
或>=
一个可能的修复是:
for (i = 0; text = texts.snapshotItem(i); i += 1) {
to
for (i = 0; text == texts.snapshotItem(i); i += 1) {