Created
April 6, 2024 14:39
-
-
Save stav/93631555a2e91c2246f5c06520026994 to your computer and use it in GitHub Desktop.
Scrape Script - Save browser document contents to local file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(async () => { | |
/** | |
* Save the content of the current page to a file. | |
* | |
* (Currently only works in Chrome) | |
* | |
* @see https://developer.mozilla.org/en-US/docs/Web/API/Window/showSaveFilePicker | |
* @param {string} name | |
* @param {string} content | |
*/ | |
async function saveFile (name, content) { | |
let writableStream | |
try { | |
const options = { | |
suggestedName: name | |
} | |
const newHandle = await window.showSaveFilePicker(options) | |
writableStream = await newHandle.createWritable() | |
await writableStream.write(content) | |
} | |
catch (err) { | |
console.error(err.name, err.message) | |
} | |
finally { | |
await writableStream.close() | |
} | |
} | |
const styleTag = ` | |
<style type="text/css"> | |
.text_strong-black { | |
font-size: large; | |
font-weight: bolder; | |
border-style: dotted; | |
border-width: thin; | |
border-radius: 0.2rem; | |
} | |
.text_red { | |
color: red; | |
} | |
.text_italic { | |
font-style: italic; | |
} | |
</style> | |
`; | |
const titleElement = false | |
|| document.querySelector('#content div.all-width-block > p:nth-child(1) span.text_strong-black') | |
|| document.querySelector('#content div.all-width-block > p:nth-child(1) span') | |
|| document.querySelector('main.main h1') | |
|| document.querySelector('#notes > header > div.chapter-title > h1') | |
|| document.querySelector('title') | |
const title = titleElement.textContent?.replace('►', '').trim() | |
const index = document.querySelector('#numBlock > span:nth-child(1)')?.textContent.trim() || 99 | |
const name = `${index} ${title}.html` | |
const textElement = document.querySelector('main.main') || document.querySelector('#notes') | |
const text = textElement.outerHTML + styleTag | |
await saveFile(name, text) | |
// $('#butNext').click() | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment