How to save and export an object in the browser console?

On the cashier's website, Lottery results are displayed in the console in the form of objects.

Http://loterias.caixa.gov.br/wps/portal/loterias/landing/megasena /

I can right-click on the object line, which contains all the contest data, and then click on "Store as global variables".

A variable named "temp1" is created.

Then, I can export such data by giving another right-click on temp1 and by clicking " Save as...".

From there, an external file is generated that I can import later.

Well, what I would like is to automate this process through command lines.

Either through this medium, from storing and saving, or through some other that you believe to be simpler and more effective.

Anyway, can you give me a hand?

Thank you!!!

Author: JHenry, 2018-07-28

2 answers

There is a API that returns the results of some lotteries in JSON with this you can integrate in almost any application (ai goes from your need).

This is her link along with the documentation:

Https://confiraloterias.com.br/api/megasena /

Example of input:

Http://confiraloterias.com.br/api0/json.php?loteria=megasena&token=r9hFuFEvYXiZUuj&concurso=2062

Return

{
"concurso": {
    "numero": "2062",
    "data": "25/07/2018",
    "cidade": "POUSO REDONDO, SC",
    "local": "Caminhão da Sorte",
    "valor_acumulado": "0,00",
    "dezenas": [
        "08",
        "10",
        "15",
        "23",
        "25",
        "34"
    ],
"premiacao": {
    "sena": {
        "ganhadores": "1",
        "valor_pago": "73.450.153,75"
        },
    "quina": {
        "ganhadores": "192",
        "valor_pago": "27.128,74"
        },
    "quadra": {
        "ganhadores": "13804",
        "valor_pago": "539,04"
        }
    },
        "arrecadacao_total": "90.343.582,00"
        },
    "proximo_concurso": {
        "data": "28/07/2018",
        "valor_estimado": "3.000.000,00"
    },
    "valor_acumulado_final_cinco": "15.139.880,89",
    "valor_acumulado_final_zero": "0,00",
    "mega_virada_valor_acumulado": "40.957.415,53",
    "resultado_completo": "1"
    }
 1
Author: Tadeu Mansi, 2018-07-28 20:47:18

Answering the question of " using only commands (without mouse) save a variable or object from the console in a file ", you can import (or paste) the code below in the console itself and you can use the command console.save(), passing as first parameter the variable/object and the path of the Target file in the second parameter.

(function(console){

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)
 }
})(console)

Usage example (in firefox/chrome console):

var a1 = "aaaaaaaaaaaaa";
console.save(a1, "/home/myUser/saida.txt");
 1
Author: res, 2018-08-02 18:53:22