canvas screenshot to png

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

目前為 2021-10-20 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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.3
// @grant       none
// @run-at      document-start
// ==/UserScript==
 
(function(
linkTag,
Date,
toISOString,
appendChild,
removeChild,
getKey,
createObjectURL,
revokeObjectURL,
setHref,
setDownload,
bodyList,
click,
consoleError,
canvasList,
toBlob
){
	"use strict";
	bodyList.__proto__=null;
	canvasList.__proto__=null;
	function saver(Blob){
		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];
		if(i)appendChild(i,linkTag);
		click(linkTag);
		if(i)removeChild(i,linkTag);
		revokeObjectURL(Blob);
	}
	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,
document.getElementsByTagNameNS("http://www.w3.org/1999/xhtml","canvas"),
Function.prototype.call.bind(HTMLCanvasElement.prototype.toBlob)
);