RED Cover Inspector

Adds simple cover preview label if needs updating for excessive size or unsupported host

目前為 2020-09-21 提交的版本,檢視 最新版本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         RED Cover Inspector
// @namespace    https://greasyfork.org/users/321857-anakunda
// @version      1.0
// @description  Adds simple cover preview label if needs updating for excessive size or unsupported host
// @author       Anakunda
// @copyright    2020, Anakunda (https://greasyfork.org/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);

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 isProxied = img.src.startsWith('https://redacted.ch/image.php?'),
		  isPreferredHost = img.src.startsWith('https://ptpimg.me'),
		  isSizeOK = !(size > acceptableCoverSize * 2**10);
	if (isPreferredHost && isSizeOK) return;
	let div = document.createElement('div');
	div.innerHTML = (isSizeOK ? '<span>' : '<span style="color: yellow;">') + formattedSize(size) +
	  '</span> / <span>' + img.naturalWidth + '×' + img.naturalHeight + '</span>';
	if (isProxied) div.innerHTML = '<span style="color: yellow;">[PROXY]</span> / ' + div.innerHTML;
		else if (!isPreferredHost) div.innerHTML = '<span style="color: yellow;">[EXTERNAL]</span> / ' + 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);
  });
});