04/02/2016 Development / java script

window.onload = function() {
    var n, div, worker;
    
    var wikis = [
        "brownie",
        "lemon",
        "chocolate",
        "sesame",
        "lollipop",
        "oat",
        "cake",
        "gingerbread",
        "sweet",
        "applicake",
        "apple",
        "chups"
    ];

    for (n = 0; n < 5; ++n) {
        worker = new Worker("task.js");
        div = document.getElementById("text-" + n);
        worker.onmessage = function (event, index) {
          var d = event.data[0];
          document.getElementById("text-" + event.data[1]).innerHTML = d;
        };
        worker.postMessage([div.innerHTML, wikis, n]);
    }
}

task.js

this.onmessage = function(e) {
    var html = e.data[0];
    var index = e.data[2];
    for(var i=0; i<e.data[1].length;i++){
        var reg = new RegExp(e.data[1][i], 'gi');
        html = html.replace(reg, "<span class='wiki'>" + e.data[1][i] + "</span>");
    }
    this.postMessage([html, index]);
}


<div id="text-0">esse eh o texto lemon, muito legal, cake</div>
<div id="text-1">applicake, apple</div>
<div id="text-2">sweet, brownie</div>
<div id="text-3">chocolate legal, cake, gingerbread</div>
<div id="text-4">brownie lemon, muito legal, cake</div>

.wiki {color: red}