0

I'm trying to use the urls from a Google results page to get info of each url, using a Chrome Extension I developed.

const items = document.querySelectorAll('div[jscontroller][jsaction][data-ved]');
var api = 'https://myapi.test';
var n = 0;
items.forEach(item => {
    var url = item.querySelector('a');
    if (url == null)
        return;
    var href = url.getAttribute("href");
    if (href == null || href.startsWith('/search') || href.includes('#') || href.startsWith('/preferences'))
        return;
    
    fetch(api + 'url_info', {
        method: 'POST',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            'url': encodeURIComponent(href)
        })
    })
        .then(function(res) {
            //wait for response..
            if (res.status !== 200) {
                console.log(res);
                return;
            }
        })
        .catch(function (error) {
            console.error(error);
        });
});

My manifest looks like this :

{


"manifest_version": 2,
  "name": "Test",
  "description": "Test extension",
  "version": "1.0.0",
  "permissions": [
    "https://myapi.test/*",
    "tabs",
    "notifications"
  ],
  "icons": {
    "128" : "img/icon_128.png"
  },
  "content_scripts": [
    {
      "js": ["content_script.js"],
      "matches": ["https://www.google.com/search*"]
    }
  ]
}

I get the url of each result but can't actually use them in order to fetch info. Either I get a CORS policy block from Google, either I get empty results using the method "no-cors" :

"Access https://myapi.test from origin 'https://www.google.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check".

Do I miss something important to pass this CORS policy that prevent me from using fetch ?

Thanks

Dunnbar
  • 11
  • 1
  • 4
  • edit your question including manifest file and any error you get if you want to have more chance to get an answer. – Robbi Feb 11 '23 at 12:51
  • Done, I've added both manifest and error from fetch. – Dunnbar Feb 11 '23 at 19:07
  • Try making the request within a background script. So first define a background script then send a message to the background which once received that message will do the fetch command. – Robbi Feb 12 '23 at 10:49

0 Answers0