Sending and receiving JSON objects from Tampermonkey

Post your working scripts, libraries and tools
jim7181812
Posts: 16
Joined: 10 Jun 2016, 10:46

Sending and receiving JSON objects from Tampermonkey

14 Feb 2018, 08:43

I've got a Tampermonkey script with multiple json objects. I'm wondering what the easiest way is to send them back and forth to my AHK is? All I'm seeing is rather complex libraries that dont discuss how to import and export them from the Tampermonkey user-script.
User avatar
gregster
Posts: 1053
Joined: 30 Sep 2013, 06:48

Re: Sending and receiving JSON objects from Tampermonkey

14 Feb 2018, 09:52

What does it mean that the script has multiple json objects? As long as these objects exist only in the script's memory, I doubt there is an easy way to access them via AHK.

If there are present as text files while the script runs, you can use FileRead/FileAppend to read/write the strings. With Coco's JSON library, for example, you then can transform the strings to an AHK object and vice versa to modify them.

Besides that, the importing/exporting from/to Tampermonkey seems rather a question for the Tampermonkey community/the script author, although someone here might have an insight into that, nonetheless... My understanding of Tampermonkey is that is used for javascript and you have access to the source code of your script and can change it? I would assume, it is then possible to add some javascript to write the objects to text files like already suggested here: https://autohotkey.com/boards/viewtopic ... 81#p174670

Btw, this is the wrong subforum for a question like this. This subforum is for complete and ready-to-use AHK functions and scripts. 'Ask for help' would be the right forum, but I assume that a mod will move it soon...
jim7181812
Posts: 16
Joined: 10 Jun 2016, 10:46

Re: Sending and receiving JSON objects from Tampermonkey

14 Feb 2018, 10:11

I can save them to the localstorage if I wanted to. I was kind of hoping there was an easy way to send them back and forth to an AHK script. These json libraries are a bit much and that is where I'm getting lost in the process. I'm the script author and I can edit the script as needed. I'm not sure where the json objects are stored. I use the TM script to scrape data off a webpage and convert it to json with JSON.stringify() I guess its just in the scripts memory.
User avatar
gregster
Posts: 1053
Joined: 30 Sep 2013, 06:48

Re: Sending and receiving JSON objects from Tampermonkey

14 Feb 2018, 10:34

Well, the easiest should be text file export/import, as far as I know.

Then, with Coco's lib ( https://autohotkey.com/boards/viewtopic.php?t=627) you only need JSON.Load(...) and JSON.Dump(...), after loading the JSON string from the text file. The Dump method is similar to 'stringify', dumping the JSON object into a string, while Load does the opposite and loads a JSON string into an AHK object. That shouldn't be the problem. But these assume that you already have the JSON as a string in a variable (which you can get easily from a text file).

What you have to figure out is the Javascript part. You should probably be able to write the result of stringify() to a file.
Of course, there is often more than one way. So, a Tampermonkey/Javascript expert can perhaps suggest alternatives... and finally, you could probably do the webscraping directly with AHK, eliminating the need for exchanging the json objects ;) Perhaps the scraping is nicer with Tampermonkey, but if want to process it with AHK, anyway, it might make sense to do the whole thing in AHK.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 14 guests