Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download images plugin #481

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
All urls will have a name (#492)
aashu0148 authored Jan 4, 2021
commit 8802da1f867a9ce09a6fc021b512f66d9a027bf7
50 changes: 42 additions & 8 deletions app/plugins/download-images.js
Original file line number Diff line number Diff line change
@@ -3,22 +3,56 @@ export const commands = [
'download-all-images',
]

export default async function() {
export default async function () {
if (window.showDirectoryPicker === undefined) {
console.log('no directory support :(')
return;
}
const dirHandle = await window.showDirectoryPicker()
const imgs = document.querySelectorAll("img")
let i = 0

imgs.forEach(async (img) => {
const url = img.src
const name = `img-${i}.png`
i++

const name = url.substr(url.lastIndexOf('/') + 1);
try {
console.log(`Fetching ${url}`)
const response = await fetch(url)
const file = await dirHandle.getFileHandle(name, { create: true })
const writable = await file.createWritable()
await response.body.pipeTo(writable)
} catch (err) {
console.error(err)
}
})
// CSS urls
let css_urls = [...document.styleSheets]
.filter(sheet => {
try { return sheet.cssRules }
catch { }
})
.flatMap(sheet => Array.from(sheet.cssRules))
.filter(rule => rule.style)
.filter(rule => rule.style.backgroundImage !== '')
.filter(rule => rule.style.backgroundImage !== 'initial')
.filter(rule => rule.style.backgroundImage.includes("url"))
.reduce((urls, { style }) => {
urls.push(style.backgroundImage);
return urls;
}, []);

console.log(`Saving to ${name}`)

css_urls.forEach(async (item, i) => {
let cots = false;
let start = item.indexOf('(');
if (item.charAt(start + 1) == '"') cots = true;
let end = item.lastIndexOf(')');
if (cots) {
start += 2;
end -= 6;
}
const url = item.substr(start, end);
const truncated = url.substr(0, url.indexOf("?"));
const name = truncated.slice(truncated.lastIndexOf("/") + 1, truncated.lastIndexOf("/") + 39) + ".jpg"
try {
const response = await fetch(url)
const file = await dirHandle.getFileHandle(name, { create: true })
const writable = await file.createWritable()
await response.body.pipeTo(writable)