Showing posts with label greasemonkey. Show all posts
Showing posts with label greasemonkey. Show all posts

Wednesday, March 30, 2016

Using javascript to save XHR file

Leave a Comment

I would like to save an JSON file which is requested on an after-login page with a POST request. Because there is no API available, I would like to download the response of an script called when loading the page as JSON file using Greasemonkey.

The page is like domain.com/map, which calls domain.com/map/script during the load. The response of domain.com/map/script is the actual response I would like to save.

1 Answers

Answers 1

full demo code below:

(function (fetch, console) {        fetch('https://api.stackexchange.com/2.2/questions/36132760?site=stackoverflow')          .then(res => res.json())          .then(data => console.save(data));        console.save = function (data, filename) {                if (!data) {                  console.error('Console.save: No data')                  return;              }                if (!filename) filename = 'console.json'                if (typeof data === "object") {                  data = JSON.stringify(data, undefined, 4);              }                var blob = new Blob([data], {type: 'text/json'}),                  e    = document.createEvent('MouseEvents'),                  a    = document.createElement('a');                a.download = filename;              a.href = window.URL.createObjectURL(blob);              a.dataset.downloadurl =  ['text/json', a.download, a.href].join(':');              e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);              a.dispatchEvent(e);      };    }).call(this, window.fetch, window.console || {});

checkout devtools-snippets#console-save

Read More

Saturday, March 26, 2016

Function infinite loop and ignoring parts of other functions

Leave a Comment

Okay, so basically all I wrote this script to do is clear and click a button if the textbox is full and refresh the page if its not.

I can successfully clear the text box when its full and refresh the page when its not, but as soon as I try to use my clickButton function it kicks into an infinite loop and skips the if() in clrLog

function addFunction(func, exec) {    var script = document.createElement('script');    script.textContent = '-' + func + (exec ? '()' : '');    document.body.appendChild(script);    document.body.removeChild(script);  }  function clickButton(val) {    buttons = document.getElementsByTagName('INPUT');    for (var i = 0; i < buttons.length; i++)    {      if (buttons[i].type == 'submit' && buttons[i].value == val)      {        buttons[i].click();      }    }  }  function clrLog() {    var elements = [    ];    elements = document.getElementsByClassName('logarea');    if (elements.log.value === '')    setTimeout(function () {      location.reload();    }, 5000);    for (var i = 0; i < elements.length; i++) {      elements[i].value = '';    }    clickButton('Edit log file');  }  function main() {    addFunction(clrLog(), true);  }  main();

1 Answers

Answers 1

I found out that I could avoid using a for loop by using document.querySelector(); instead - so much easier :)

Read More