0

Disclaimer : I'm a dick in JS, I really don't know it at all, sorry if this is really a noob question.

I found out a userscript that allows you to replace words/phrases on a webpage. Howevever, I have an error that states "expected a conditional expression and instead saw an assignment" on line 57

The line is :

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

The whole script :

(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]);
        } );
    }
}
} () );

Can someone explain me what it means (I like to understand what I do), and how I can fix this ?

SBO
  • 423
  • 1
  • 6
  • 17

1 Answers1

3

The = is an assignment operator.

Assignment is not valid in this context, because the for loop expects a condition.

You need to use a comparison operator such as ==, <= or >=

One possible fix is:

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

to

for (i = 0; text == texts.snapshotItem(i); i += 1) {
HumbleWebDev
  • 555
  • 4
  • 20