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