Skip to content

Instantly share code, notes, and snippets.

@tsweeper
Created August 27, 2019 10:54
Show Gist options
  • Select an option

  • Save tsweeper/cd3ffe980c5393a738226da8cb652c40 to your computer and use it in GitHub Desktop.

Select an option

Save tsweeper/cd3ffe980c5393a738226da8cb652c40 to your computer and use it in GitHub Desktop.
Convert and extract canvas contents into image
/// Convert canvas content into image.
/// Usage: Run this script in console.
/***************************************************************
* Warning Notice
*
* This script is provided for EDUCATIONAL PURPOSES ONLY.
* Do NOT use this script for illegal or copyright infringement activities.
*
* This script is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This notice MUST APPEAR in all copies of the script!
***************************************************************/
// create new window with blank document
var w = window.open("", document.title);
// create new document
var newDoc = document.implementation.createHTMLDocument("New Document");
newDoc.body.style = "margin: 0px; background: #0e0e0e;";
newDoc.title = document.title;
// add front image if found
try {
var frontimg = document.getElementsByClassName('front-link-page')[0].querySelectorAll('img')[0];
frontimg.style = "display: block;margin: auto;";
newDoc.body.appendChild(frontimg);
}
catch {
console.log("front-link-page not found");
}
// get every canvas then convert into array to use forEach
var canvasimgs = document.getElementsByTagName('canvas');
Array.from(canvasimgs).forEach(
function(currentValue, currentIndex) {
// prevent exception if canvas is blank
try {
// create new img element
var img = newDoc.createElement("img");
img.src = currentValue.toDataURL('image/png');
img.style = "display: block;margin: auto;";
img.id = "id" + currentIndex;
// attach img to the document
newDoc.body.appendChild(img);
} catch {
return;
}
});
// swap document contents
w.document.replaceChild(
w.document.importNode(newDoc.documentElement, true),
w.document.documentElement
);
@tsweeper

tsweeper commented Nov 2, 2021

Copy link
Copy Markdown
Author

please describe your problem.
the script collects 'canvas' tags, then try convert it into images, then returns new html pages with converted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment