Advertisement
Shuraken007

kakao_saver.js

Mar 25th, 2025 (edited)
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Kakao Saver
  3. // @namespace    http://tampermonkey.net/
  4. // @version      2025-03-25
  5. // @description  try to take over the world!
  6. // @author       Shuraken
  7. // @match        https://page.kakao.com/content/*/viewer/*
  8. // @icon         https://www.google.com/s2/favicons?sz=64&domain=kakao.com
  9. // @grant        none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13.     'use strict';
  14.  
  15.     async function delay(ms) {
  16.         return new Promise(resolve => setTimeout(resolve, ms))
  17.     }
  18.  
  19.     function get_paragraphs() {
  20.         let root = [...document.querySelectorAll('div')].filter(x => x.shadowRoot)
  21.         if (root.length === 0) return null
  22.         let paragraphs = [...root[0].shadowRoot.firstChild.getElementsByTagName('p')]
  23.         if (paragraphs.length === 0) return null
  24.         return paragraphs
  25.     }
  26.  
  27.     function get_book_chapter() {
  28.       let path_arr = window.location.href.split('/')
  29.       let chapter = path_arr.pop()
  30.       path_arr.pop()
  31.       let book = path_arr.pop()
  32.       return [book, chapter]
  33.     }
  34.  
  35.     function save_paragraphs(paragraphs) {
  36.         let [book, chapter] = get_book_chapter()
  37.         let storage = localStorage.getItem(`kakao_saver_${book}`) || '{}'
  38.         storage = JSON.parse(storage)
  39.         storage[chapter] = paragraphs.map(x => x.textContent).join('\n')
  40.         localStorage.setItem(`kakao_saver_${book}`, JSON.stringify(storage))
  41.         console.log(`saved ${paragraphs.length} paragraphs for ${chapter} chapter`)
  42.     }
  43.  
  44.     function kk_print() {
  45.         let [book, _] = get_book_chapter()
  46.         let storage = localStorage.getItem(`kakao_saver_${book}`) || '{}'
  47.         storage = JSON.parse(storage)
  48.         let sorted_pairs = Object.entries(storage).sort(([, a], [, b]) => Number(b) - Number(a))
  49.         if (sorted_pairs.length === 0) {
  50.            console.log("nothing to print, no chapters")
  51.            return
  52.         }
  53.         let chapters = []
  54.         for (let [_, val] of sorted_pairs)
  55.             chapters.push(val)
  56.         console.log(chapters.join("\n\n"))
  57.         console.log(`printed ${chapters.length} chapters`)
  58.         console.log(`first started with ${chapters[0].substring(0, 10)}`)
  59.         console.log(`last ended with ${chapters[chapters.length - 1].slice(-10, -1)}`)
  60.     }
  61.     window.kk_print = kk_print
  62.  
  63.     async function run() {
  64.        let paragraphs;
  65.        while(!paragraphs) {
  66.             paragraphs = get_paragraphs()
  67.            await delay(200)
  68.        }
  69.        save_paragraphs(paragraphs)
  70.     }
  71.  
  72.     run()
  73.  
  74.     window.navigation.addEventListener("navigate", () => run())
  75. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement