javascriptgreasemonkeytampermonkeygreasemonkey-4gm-xmlhttprequest

Is it possible to use GM_xmlhttpRequest to my data form using greasemonkey/tampermonkey?


I am currently working on GM_getvalue but it only saves data on the local storage. I wanted to save the inputted values to my server where send.php is located.

This is my code:

var $ = unsafeWindow.jQuery;

$(document).ready(function() {

    if($("#save_form").html()){
        $("#save_form").submit(function(){
            var fullname = $("#name").val();
            var IDnumber = $("#id").val();
            GM_setValue("attendancelogs",GM_getValue("attendancelogs","")+fullname+" "+IDnumber+"<br/>");
        });
    }

Someone suggested me to use GM_xmlhttpRequest but i have no idea how to use it. He told me GM_xmlhttpRequest looks like this:

jQ(document).on("keyup", "form input", function () {
    let value = GM_getValue("name_full","");
    GM_xmlhttpRequest({
        method: "POST",
        url: "http://....",
        data: value,
        headers: {
            "Content-Type": "application/x-www-form-urlencoded"
        },
        onload: function(response) {
            alert(response);
            var json = $.parseJSON(response); 
        }
    });

And lastly, what would be the send.php code?


Solution

  • GM_xmlHttpRequest is just an implementation of XHR that can bypass SOP.

    You can find the documentation for XHR in general, or some examples.

    You will of course need to add the following to your script header:

    // @grant GM.xmlHttpRequest