Я создал плагин Chrome, который распознает детектор неработающих ссылок с выделением фона привязки красным цветом.Для некоторых ссылок, даже если их присутствие в том же домене или внешней ссылке отображается как ошибка CORS.
Найдите приведенный ниже код.
document.getElementById("test").addEventListener('click', () => {
console.log("Popup DOM fully loaded and parsed");
function CheckLinks(lnks) {
Object.keys(lnks).map(function(key) {
return [lnks[key].href, lnks[key].parentElement];
}).map((a) => {
if(a[0].indexOf('javascript') === -1 && a[0].indexOf('mailto') === -1){
UrlExists(a[0], 'GET', '', (exists) => {
if(!exists){
a[1].style.background = "Red";
}
}, (errorText) => {
console.log(errorText);
});
}
});
}
function modifyDOM() {
function UrlExists(url, method, data, callback, errback) {
var req;
if(XMLHttpRequest) {
req = new XMLHttpRequest();
if('withCredentials' in req) {
req.open(method, url, true);
req.onerror = errback;
req.onreadystatechange = function() {
if (req.readyState === 4) {
if (req.status >= 200 && req.status < 400) {
callback(true);
} else {
callback(false);
}
}
};
req.setRequestHeader("Access-Control-Allow-Origin",true);
req.send(data);
}
} else if(XDomainRequest) {
req = new XDomainRequest();
req.open(method, url);
req.onerror = errback;
req.onload = function() {
callback(true);
};
req.send(data);
} else {
errback('CORS not supported');
}
}
//You can play with your DOM here or check URL against your regex
console.log('Tab script:');
console.log(document.links);
Object.keys(document.links).map(function(key) {
return [document.links[key].href, document.links[key].parentElement];
}).map((a) => {
if(a[0].indexOf('javascript') === -1){
UrlExists(a[0], 'GET', '', (exists) => {
if(!exists){
a[1].style.background = "Red";
}
}, (errorText) => {
console.log(errorText);
});
}
});
}
//We have permission to access the activeTab, so we can call chrome.tabs.executeScript:
chrome.tabs.executeScript({
code: '(' + modifyDOM + ')();' //argument here is a string but function.toString() returns function's code
}, (results) => {
//Here we have just the innerHTML and not DOM structure
console.log('Popup script:')
console.log(results[0]);
//CheckLinks(results[0]);
});
});
Некоторые из ошибок
Ответ на запрос предварительной проверки не проходит проверку контроля доступа: в запрошенном ресурсе отсутствует заголовок «Access-Control-Allow-Origin».
Не удалось загрузить https://portal.office.com/account/#security: Ответ дляпредпечатная проверка недействительна (перенаправление)