Skip to content

Instantly share code, notes, and snippets.

@oddlyfunctional
Last active August 10, 2023 00:16

Revisions

  1. oddlyfunctional revised this gist Aug 10, 2023. 1 changed file with 13 additions and 7 deletions.
    20 changes: 13 additions & 7 deletions biblioteca-nacional.js
    Original file line number Diff line number Diff line change
    @@ -7,12 +7,14 @@ const downloadURI = (uri, name) => {
    document.body.removeChild(link);
    }

    const title = document.getElementById('PastaTxt')
    .title
    .replaceAll('\\', '-')
    .replaceAll(' ', '-')
    const downloaded = new Set()

    function downloadAndLoadNext() {
    const title = document.getElementById('PastaTxt')
    .innerText
    .replaceAll('\\', '-')
    .replaceAll(' ', '-')

    const [currentMatch, lastMatch] = document.getElementById('OcorNroLbl')
    .innerText
    .split('/')
    @@ -21,8 +23,12 @@ function downloadAndLoadNext() {
    const currentUrl = document.getElementById('DocumentoImg').src
    const nextPage = document.getElementById('OcorPosBtn')

    const currentPage = document.getElementById('PagAtualTxt').value
    downloadURI(currentUrl, `${title}-Página-${currentPage}.jpg`)
    if (!downloaded.has(currentUrl)) {
    downloaded.add(currentUrl)

    const currentPage = document.getElementById('PagAtualTxt').value
    downloadURI(currentUrl, `${title}-Página-${currentPage}.jpg`)
    }

    if (currentMatch === lastMatch) {
    alert('Terminou!')
    @@ -43,4 +49,4 @@ function downloadAndLoadNext() {
    }
    }

    downloadAndLoadNext()
    downloadAndLoadNext()
  2. oddlyfunctional revised this gist Aug 10, 2023. 1 changed file with 14 additions and 10 deletions.
    24 changes: 14 additions & 10 deletions biblioteca-nacional.js
    Original file line number Diff line number Diff line change
    @@ -7,23 +7,27 @@ const downloadURI = (uri, name) => {
    document.body.removeChild(link);
    }

    const title = document.getElementById('PesquisarTxt').value
    .replaceAll('"', '')
    const title = document.getElementById('PastaTxt')
    .title
    .replaceAll('\\', '-')
    .replaceAll(' ', '-')
    const lastPage = Number(document.getElementById('PagTotalLbl').innerText.replace('/', ''))

    function downloadAndLoadNext() {
    const currentPageInput = document.getElementById('PagAtualTxt')
    const [currentMatch, lastMatch] = document.getElementById('OcorNroLbl')
    .innerText
    .split('/')
    .map(Number);

    const currentUrl = document.getElementById('DocumentoImg').src
    const nextPage = document.getElementById('OcorPosBtn')

    const currentPage = Number(currentPageInput.value)
    downloadURI(currentUrl, `${title}-${currentPage}.jpg`)
    const currentPage = document.getElementById('PagAtualTxt').value
    downloadURI(currentUrl, `${title}-Página-${currentPage}.jpg`)

    if (currentPage === lastPage) {
    if (currentMatch === lastMatch) {
    alert('Terminou!')
    } else {
    currentPageInput.value = currentPage + 1
    currentPageInput.onchange()
    nextPage.click()

    function waitUntilLoaded() {
    const newUrl = document.getElementById('DocumentoImg').src
    @@ -39,4 +43,4 @@ function downloadAndLoadNext() {
    }
    }

    downloadAndLoadNext()
    downloadAndLoadNext()
  3. oddlyfunctional created this gist Aug 9, 2023.
    42 changes: 42 additions & 0 deletions biblioteca-nacional.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    const downloadURI = (uri, name) => {
    const link = document.createElement("a");
    link.download = name;
    link.href = uri;
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    }

    const title = document.getElementById('PesquisarTxt').value
    .replaceAll('"', '')
    .replaceAll(' ', '-')
    const lastPage = Number(document.getElementById('PagTotalLbl').innerText.replace('/', ''))

    function downloadAndLoadNext() {
    const currentPageInput = document.getElementById('PagAtualTxt')
    const currentUrl = document.getElementById('DocumentoImg').src

    const currentPage = Number(currentPageInput.value)
    downloadURI(currentUrl, `${title}-${currentPage}.jpg`)

    if (currentPage === lastPage) {
    alert('Terminou!')
    } else {
    currentPageInput.value = currentPage + 1
    currentPageInput.onchange()

    function waitUntilLoaded() {
    const newUrl = document.getElementById('DocumentoImg').src

    if (newUrl === currentUrl) {
    setTimeout(waitUntilLoaded, 0)
    } else {
    setTimeout(downloadAndLoadNext, 0)
    }
    }

    waitUntilLoaded()
    }
    }

    downloadAndLoadNext()