1

I'm writing a Chrome extension that needs to change/add link to matching words. It gets the word from what is stored inside contentscript on specific pages I have chosen.

My extension won't work and I don't understand what's wrong. My coding is not good. Would love to see it work.

The most important from manifest.json:

{
   "content_scripts": [ {
      "all_frames": true,
      "js": [ "jquery.min.js", "contentscript.js" ],
      "matches": [ "*://portal.example.no/*", "*://*.portal.example.no/*" ],
      "run_at": "document_end"
   } ],
   "permissions": [ "*://portal.example.no/*", "*://*.portal.example.no/*", "activeTab", "storage" ],
}

The whole contentscript.js:

const data = [
    //place
    {
        word: "Rom: A1-030",
        url: "https://tv.example.com/64318737"
    },
    {
        word: "Rom: B2-010",
        url: "https://tv.example.com/64318841"
    },
];


    $.get(data).appendTo($('body'));  })
     startWorkLoop();


function getUrl(rom) {
    let found = data.filter(d => {
        return d.word === rom;
    });
    if (found.length)
        return found[0].url;
    else
        return false;
}


function startWorkLoop(){
    setInterval(function(){
        work();
    }, 1500);
}

async function work() {
    let all = document.body.querySelectorAll("span");
    if(){
        $('#ext-unpaid-popup').show();
    }else {
        $('#ext-unpaid-popup').hide();
    }
    for (let i = 0; i < all.length; i++) {
        let el = all[i];
        if (el.innerHTML) {
            let content = el.innerHTML;

            let match = `${content}`.match(/Rom: [A-Z][0-9]-[0-9]+/gmi);
            if (match && !el.classList.contains("rom-ext-done")) {
                el.classList.add("rom-ext-done");
                let m = match[0];
                    let url = getUrl(m);
                    if (url) {
                        content = content.replace(m, `<a style="background: green;color: #fff; padding: 2px 3px" href="${ ? url : '#'}">${m}</a>`);
                    } else {
                        content = content.replace(m, `<a style="background: red;color: #fff; padding: 2px 3px">${m} No streaming</a>`);
                    }
                el.innerHTML = content;
            }

        }
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
  • Sounds like `tv.example.com` is a cross-origin URL for `portal.example.no` so your content scripts won't be able to make a network request. Do it in the background script, [example](https://stackoverflow.com/a/55292071). – wOxxOm Jan 21 '21 at 06:29

0 Answers0