It must be something like this:
On the server there should be a script that will receive the request and give the answer.
<?php header("Access-Control-Allow-Origin: *"); if ($_GET["id"]) { echo "ID" . $_GET["id"] . "присутствует в базе!"; }
The above is a primitive example. Set the header Access-Control-Allow-Origin . Its installation means that the server allows a cross-domain request. If it does not exist, the browser will not receive it and will terminate the request with an error.
Next, we perform a primitive id check, which will be sent from the extension.
Checking the GET array. Yeah, id passed. Display a message. That is, the echo construction output will be transmitted as an answer.
In fact, you will have advanced processing. Ie, do not just look at the presence of GET["id"] , but check for the presence in the file and so on.
The extension should have something like:
var xmlHttpRequest = (function () { var _xmlHttpRequest = new XMLHttpRequest(); function sendRequest(url) { return new Promise(function (resolve, reject) { _xmlHttpRequest.onload = function () { resolve(_xmlHttpRequest.responseText); }; _xmlHttpRequest.onerror = function () { reject(_xmlHttpRequest.statusText); }; _xmlHttpRequest.open("get", url, true); _xmlHttpRequest.send(); }); } return { sendRequest: sendRequest } })();
You can call this whole thing like this:
xmlHttpRequest.sendRequest("http://localhost?id=1").then(function (result) { alert(result); });
Replace the sendRequest function sendRequest with your own. I checked on my computer, so I specified localhost .
Here we send a get request to the specified address and wait for the result of the execution. As soon as a response is received from the server, you will see a pop-up window.
References:
XMLHttpRequest Basics
XMLHttpRequest: cross-domain requests
JavaScript promises (this is about new Promise in the code above)
UPD
take data from txt file, there is one big line with digits separated by comma
Well, then do something like:
if($_GET["id"]) { $data = file_get_contents("data.txt"); $data = explode(",", $data); if(array_search($_GET["id"],$data) !== false ) { echo "ID" . " " . $_GET["id"] . " " . "присутствует в файле"; } }
See if there is an id . If there is, then read the file. Convert the contents of the file into an array ( explode converts a string into an array using the separator "," ).
Next, we check the presence of the incoming id in the array using the function array_search .
References:
array_search
explode
but this is what the browser console displays when promising {Promise {: "pending"}
If you read what I dropped you might have noticed:
A promise can be: fulfilled - successfully completed rejected - completed with a pending error - not completed settled - completed with any outcome
State diagram Image More about promise