canvas screenshot to png

useful for io games like woomy.arras.io (press [:])

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        canvas screenshot to png
// @namespace   http://bzzzzdzzzz.blogspot.com/
// @description useful for io games like woomy.arras.io (press [:])
// @author      BZZZZ
// @include     *
// @version     0.5
// @grant       none
// @run-at      document-start
// @inject-into content
// ==/UserScript==
 
(function(
linkTag,
Date,
toISOString,
appendChild,
removeChild,
getKey,
createObjectURL,
revokeObjectURL,
setHref,
setDownload,
bodyList,
click,
consoleError,
consoleWarn,
canvasList,
toBlob
){
	"use strict";
	bodyList.__proto__=null;
	canvasList.__proto__=null;
	function saver(Blob){
		try{
			Blob=createObjectURL(Blob);
			setHref(linkTag,Blob);
			var raw=toISOString(new Date()),l=raw.length,c,i=0,w="shot_";
			while(i<l)w+=((c=raw[i++])==":")?"_":c;
			w+=".png";
			setDownload(linkTag,w);
			i=bodyList[0];
			try{
				if(i)appendChild(i,linkTag);
			}catch(err){
				consoleWarn("canvas screenshot to png: failed to append a to body:",err);
			}
			click(linkTag);
			try{
				if(i)removeChild(i,linkTag);
			}catch(err){
				consoleWarn("canvas screenshot to png: failed to remove a from body:",err);
			}
			revokeObjectURL(Blob);
		}catch(err){
		
		}
	}
	window.addEventListener("keypress",function(evnt){
		try{
			if(getKey(evnt)!=":")return;
			var i=canvasList[0];
			if(!i){
				consoleError("canvas screenshot to png: no canvas tag");
				return;
			}
			toBlob(i,saver,"image/png");
		}catch(err){
			consoleError("canvas screenshot to png:",err);
		}
	},false);
})(
document.createElementNS("http://www.w3.org/1999/xhtml","a"),
Date,
Function.prototype.call.bind(Date.prototype.toISOString),
Function.prototype.call.bind(Node.prototype.appendChild),
Function.prototype.call.bind(Node.prototype.removeChild),
Function.prototype.call.bind(KeyboardEvent.prototype.__lookupGetter__("key")),
URL.createObjectURL,
URL.revokeObjectURL,
Function.prototype.call.bind(HTMLAnchorElement.prototype.__lookupSetter__("href")),
Function.prototype.call.bind(HTMLAnchorElement.prototype.__lookupSetter__("download")),
document.getElementsByTagNameNS("http://www.w3.org/1999/xhtml","body"),
Function.prototype.call.bind(HTMLElement.prototype.click),
console.error,
console.warn,
document.getElementsByTagNameNS("http://www.w3.org/1999/xhtml","canvas"),
Function.prototype.call.bind(HTMLCanvasElement.prototype.toBlob)
);