Save data using Greasemonkey/Tampermonkey for later retrieval

11,017

I know I can't write to a file on my PC

Here's some good news for you: Yes you can!

var a = document.createElement("a");
a.href = "data:text,Hello World!";   //content
a.download = "Hello.txt";            //file name
a.click();

http://jsfiddle.net/DerekL/jgfhwfL0/


First open up your localhost page, master.html (http://localhost:8080/master.html):

<html>
    <head>
        <script>
            window.addEventListener("load", function(){
                //This is just like working with threads but in JavaScript
                if(location.search.length){
                    //receiver
                    var query = decodeURIComponent(location.search.slice(1));
                    var data = JSON.parse(query).value;

                    //handle data
                    localStorage.storage = +localStorage.storage + data;

                    location.href = "about:blank";  //exit
                }else{
                    //master
                    sum = document.getElementById("sum"); 
                    sum.innerText = localStorage.storage = "0";

                    window.addEventListener("storage", function(){
                        //data received from receiver
                        sum.innerText = localStorage.storage;
                    });
                }
            });
        </script>
    </head>
    <body>
        Sum: <span id="sum"></span>
    </body>
</html>

Then you can start sending data to it in any webpages:

var frame = document.createElement("iframe");
frame.src = 'http://localhost:8080/master.html?{"value":90}';    //port 8080
document.body.appendChild(frame);

The sum counter should update automatically upon receiving data.

Share:
11,017
AvZ
Author by

AvZ

Bash has all the answers avz@PC-Linux:~$ whatis love love: nothing appropriate. Mathematics and Physics are my favourite subjects. I also like to code a little here and then (In C++, Python, or Javascript usually). I log on to Mathematics SE whenever I can to find nice problems to solve. Got first rank change for 1st Quarter 2015 quarter for Mathematics SE! Can't believe it!

Updated on June 24, 2022

Comments

  • AvZ
    AvZ almost 2 years

    I am working on a project which requires data from a website. The problem with that data is that it is organised into a lot of different webpages with serial numbering in the URL. To tackle this problem, I wrote a simple script in Tampermonkey which loops through those pages and obtains the data in a string variable in the script.

    Now comes the real problem, how do I store this data. I know I can't write to a file on my PC but can the data be displayed onto a separate tab in my browser so that I can copy and paste it into a local file when the loop is done? I wish to append a string into storage each loop

    I do not want to use GM_setValue because I want the data in raw text format (like a .txt file)

    However, if it can be written directly into a file on my PC without the use of an external library, that'd be preferred.