Last active
February 6, 2023 03:57
-
-
Save larvata/79559fd327eb32512abb4cd2f5530562 to your computer and use it in GitHub Desktop.
bookmarklet for saving 週プレ images
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
// using online bookmarklet tools | |
// https://www.yourjs.com/bookmarklet/ | |
function createButton(label) { | |
const btn = document.createElement('button'); | |
btn.innerHTML = label; | |
btn.style.fontSize = '50px'; | |
btn.style.position = 'absolution'; | |
btn.style.top = 0; | |
btn.style.marginTop = '100px'; | |
return btn; | |
} | |
function saveImage(url, fileName) { | |
const a = document.createElement('a'); | |
a.href = url; | |
a.download = fileName; | |
a.click(); | |
} | |
function insertDownloadButton(target) { | |
const iframes = target.querySelectorAll('iframe'); | |
window.lastIframes = iframes; | |
iframes.forEach((frm) => { | |
const frame = frm.contentDocument; | |
if (!frame) { | |
// failed to get contentDocument | |
// that mostly means video iframe was loaded | |
const btn = createButton('Open Video'); | |
btn.addEventListener('click', () => { | |
navigator.clipboard.writeText(frm.src); | |
window.open(frm.src); | |
}); | |
target.body.prepend(btn); | |
return; | |
} | |
// contains image? | |
const img = frame.querySelector('svg>image'); | |
if (img) { | |
const btn = createButton('Get Image'); | |
btn.addEventListener('click', () => { | |
const data = img.getAttribute('xlink:href'); | |
saveImage(data, 'download.png'); | |
}); | |
frame.body.prepend(btn); | |
} | |
// contains iframe? | |
insertDownloadButton(frame); | |
}); | |
} | |
insertDownloadButton(document); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment