[英]How to send XHR request only to real domains?
我正在發送XMLHttpRequest
並且只想將它發送到真實域,這些域不為空並且不是 Chrome 的內部chrome:
域。
直到現在我都失敗了 - 我的XMLHttpRequest
在任何情況下都在發送 - 但在currentDomaon == 0
或currentProtocoll == "chrome:"
情況下,它是空的。
我在做什么錯? 我應該如何調整代碼以達到目標?
var currentDomain = "";
var currentProtocol ="";
const processingTabId = {};
function run(tab) {
if (processingTabId[tab.id]) return;
processingTabId[tab.id] = true;
let newUrl = new URL(tab.pendingUrl || tab.url)
currentDomain = newUrl.hostname;
currentProtocol = newUrl.protocol;
var xhr = new XMLHttpRequest();
var protocol = "https://";
var middle = ".myservice/"
var end = "/action/data/";
xhr.open("GET", protocol + middle + currentDomain + end, true);
xhr.responseType = 'document';
if (currentDomain !== null) && (currentProtocol !== "chrome:") {
xhr.send();
}
}
您可以使用正則表達式來確定字符串是否是以 HTTP/HTTPS 開頭的有效 URL。
例如:
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)
請參閱此堆棧溢出帖子。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.