A
A
Aison2021-09-01 13:44:23
JavaScript
Aison, 2021-09-01 13:44:23

How to make js execution only on domains from the list?

I'm making a simple chrome extension. There was a problem, the extension works on all sites, but you need the js script to work on certain domains, which the script will receive from a txt file at some address.

I know that you can use the extension manifest, but it will be inconvenient to add new domains there, and it’s still easier to edit the text editor on your server than to update the extension every time.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GrayHorse, 2021-09-01
@Aison

Write a userscript instead of an extension.
Here is an example:

// ==UserScript==
// @name        Toster Example
// @match       https://qna.habr.com/*
// @match       https://toster.ru/*
// @grant       GM.xmlHttpRequest
// @connect     https://example.com
// ==/UserScript==

!async function() {
    console.log("userscript start");
    const response = await new Promise((resolve, reject) => {
        GM.xmlHttpRequest({
            method: "get",
            url: "https://example.com",
            responseType: "text",
            onload: resolve,
            onerror: reject,
        });
    });
    console.log("response:", response);

    const {response: text} = response;
    console.log("text:", text);
}();

Runs on every Toaster page (see @match), and successfully logs the contents of https://example.com to the console despite CORS.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question