Greasy Fork

Greasy Fork is available in English.

RED Cover Inspector

Adds simple cover sticker if needs updating for unsupported host / big size / small resolution

当前为 2020-09-21 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         RED Cover Inspector
// @namespace    http://greasyfork.icu/users/321857-anakunda
// @version      1.001
// @description  Adds simple cover sticker if needs updating for unsupported host / big size / small resolution
// @author       Anakunda
// @copyright    2020, Anakunda (http://greasyfork.icu/users/321857-anakunda)
// @license      GPL-3.0-or-later
// @match        https://redacted.ch/torrents.php?id=*
// @connect      *
// @grant        GM_xmlhttpRequest
// @grant        GM_getValue
// @grant        GM_setValue
// ==/UserScript==

function getRemoteFileSize(url) {
  return new Promise(function(resolve, reject) {
	var imageSize, abort = GM_xmlhttpRequest({
	  method: 'GET', url: url, responseType: 'arraybuffer',
	  onreadystatechange: function(response) {
		if (imageSize || response.readyState < XMLHttpRequest.HEADERS_RECEIVED
			|| !/^(?:Content-Length):\s*(\d+)\b/im.test(response.responseHeaders)) return;
		if (!(imageSize = parseInt(RegExp.$1))) return;
		resolve(imageSize);
		abort.abort();
	  },
	  onload: function(response) { // fail-safe
		if (imageSize) return;
		if (response.status >= 200 && response.status < 400) resolve(response.responseText.length /*response.response.byteLength*/);
			else reject('image not accessible');
	  },
	  onerror: response => { reject('image not accessible') },
	  ontimeout: response => { reject('image not accessible') },
	});
  });
}

function formattedSize(size) {
  return size < 1024**1 ? Math.round(size) + ' B'
	: size < 1024**2 ? (Math.round(size * 10 / 2**10) / 10) + ' KiB'
	: size < 1024**3 ? (Math.round(size * 100 / 2**20) / 100) + ' MiB'
	: size < 1024**4 ? (Math.round(size * 100 / 2**30) / 100) + ' GiB'
	: size < 1024**5 ? (Math.round(size * 100 / 2**40) / 100) + ' TiB'
	: (Math.round(size * 100 / 2**50) / 100) + ' PiB';
}

var acceptableCoverSize = GM_getValue('acceptable_cover_size');
if (!(acceptableCoverSize > 0)) GM_setValue('acceptable_cover_size', acceptableCoverSize = 2048);
var acceptableCoverResolution = GM_getValue('acceptable_cover_resolution');
if (!(acceptableCoverResolution > 0)) GM_setValue('acceptable_cover_resolution', acceptableCoverResolution = 200);

document.querySelectorAll('div#covers p > img').forEach(function(img) {
  getRemoteFileSize(img.src).catch(function(reason) {
	console.warn('Failed to get remote image size (' + img.src + '):', reason);
	return undefined;
  }).then(function(size) {
	const span = (content, isOK = false) => (isOK ? '<span>' : '<span style="color: yellow;">') + content + '</span>',
		  isProxied = img.src.startsWith('https://redacted.ch/image.php?'),
		  isPreferredHost = img.src.startsWith('https://ptpimg.me'),
		  isSizeOK = !(size > acceptableCoverSize * 2**10),
		  isResolutionOK = Math.min(img.naturalWidth, img.naturalHeight) >= acceptableCoverResolution;
	if (isPreferredHost && isSizeOK && isResolutionOK) return;
	let div = document.createElement('div');
	div.innerHTML = span(formattedSize(size), isSizeOK) + ' / ' +
	  span(img.naturalWidth + '×' + img.naturalHeight, isResolutionOK);
	if (isProxied) div.innerHTML = span('[PROXY]') + ' / ' + div.innerHTML;
		else if (!isPreferredHost) div.innerHTML = span('[EXT]') + ' / ' + div.innerHTML;
	div.style = 'color: white; border: 1px solid whitesmoke; background-color: #ae2300; ' +
	  'position: relative; bottom: 25px; right: 5px; font: 700 8pt "Segoe UI"; float: right; padding: 1px 5px;';
	img.insertAdjacentElement('afterend', div);
  });
});