简体   繁体   English

我怎样才能仅在一次循环后调用此函数?

[英]How can I call this function only once yet loop as needed?

I have a Chrome extension that replaces certain phrases on webpages. 我有一个Chrome扩展程序,可以替换网页上的某些短语。 It uses a 2 dimensional array. 它使用二维数组。 The [i][1] replaces the text in provided in the [i][0] value. [i] [1]替换[i] [0]值中提供的文本。

for (var i = 0; i < array.length; i++) {
    findAndReplaceDOMText(document.body, {
      preset: 'prose',
      find: array[i][0],
      replace: array[i][1]
    });
}

This code works fine yet it seems computationally expensive as it calls the findAndReplaceDOMText function multiple times rather than just once. 这段代码可以正常工作,但是它似乎多次调用findAndReplaceDOMText函数,而不是一次调用,因此在计算上似乎很昂贵。 Is it possible to move the for loop inside the function to just wrap the find and replace params? 是否可以在函数内移动for循环以仅包装findreplace参数? If so what would that look like? 如果是这样,那会是什么样? I can only get console errors trying this. 我只能在尝试此操作时收到控制台错误。

Edit: The function traverses the DOM looking for all visible human readable text that contains the regex phrase provided at find and replaces with the string provided at replace . 编辑:该函数遍历DOM,以查找所有可见的人类可读文本,这些文本包含find提供的regex短语,并替换为replace提供的字符串。

You could try to use the replace parameter as a function. 您可以尝试将replace参数用作函数。 The find -configuration-property then needs to be a regular expression ( RegExp -object) constructed of your search strings (like /first|second|third/g ). 然后, find -configuration-property必须是由您的搜索字符串(如/first|second|third/g )构成的正则表达式( RegExp -object)。 Do not forget the g -modifier at the end. 不要忘记最后的g修饰符。

As replace -configuration-property you then create a function that checks which string occurred (you get that as the second parameter to your function). 然后,作为replace -configuration-property,创建一个函数,该函数检查发生了哪个字符串(将其作为函数的第二个参数获取)。 According to the match you then return the corresponding value (for example if match is "first" then you return "1st" . If match is "second" then you return "2nd" and so on). 然后根据匹配项返回相应的值(例如,如果match"first"则返回"1st" 。如果match"second"则返回"2nd" ,依此类推)。

Without modifying the function's behaviour, you can't. 如果不修改函数的行为,则无法这样做。 What you could do is separating the find and replace passes. 可以做的是分离查找和替换通道。

Your strings are stored in a kind of difficult to transverse way, so let's flat them out: 您的弦以一种难以横穿的方式存储,因此我们将它们展平:

var flatFind = array.map(function(elem){
    return elem[0]
})

var flatReplace = array.map(function(elem){
    return elem[1]
})

Then, you'd need to create a regex string that encompasses all your search strings: 然后,您需要创建一个包含所有搜索字符串的正则表达式字符串:

var searchString = "/("+flatFind.join("|")+")/g"

Then pass it to the function, using a function to find the index of the match: 然后将其传递给函数,使用函数查找匹配项的索引:

findAndReplaceDOMText(document.body, {
    preset: 'prose',
    find: searchString,
    replace: function(portion, match){
      var idx = flatFind.indexOf(match)
      if(idx > -1) return flatReplace[idx]
      else throw "hey, there's no replacement string for this!"
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM