Skip to content

Instantly share code, notes, and snippets.

@larvata
Last active February 6, 2023 03:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larvata/79559fd327eb32512abb4cd2f5530562 to your computer and use it in GitHub Desktop.
Save larvata/79559fd327eb32512abb4cd2f5530562 to your computer and use it in GitHub Desktop.
bookmarklet for saving 週プレ images
// 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