Dead Image Size Revealer

Shows the size of broken images

目前為 2018-01-09 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name          Dead Image Size Revealer
// @namespace     DoomTay
// @description   Shows the size of broken images
// @include       *
// @version       1.0.0
// @exclude       *.svg
// @grant         none


// ==/UserScript==

var pics = document.images;

function relativeToAbsolute(relativeURL)
{
	var testLink = document.createElement("A");
	testLink.href = relativeURL;
	return testLink.href;
}

function addSize(pic) {
	if(pic && pic.src && !getDivSign(pic))
	{
		var offset = getOffset(pic);
		var sizeDiv = document.createElement("div");
		sizeDiv.innerHTML = pic.width + " x " + pic.height;
		sizeDiv.className = "imageSize";
		var bindingClass = randomString();
		sizeDiv.classList.add(bindingClass);
		pic.classList.add(bindingClass);
		pic.parentNode.insertBefore(sizeDiv, pic.nextSibling);
		sizeDiv.style.left = Math.round(offset.left) + "px";
		sizeDiv.style.top = Math.round(offset.top) + "px";
	}
}

var sizeDivStyle = document.createElement("style");
sizeDivStyle.type = "text/css";
sizeDivStyle.innerHTML = ".imageSize { background-color:#333; color: white; position: absolute; font-family: Arial; font-size: 12px; font-weight:normal }";
document.head.appendChild(sizeDivStyle);

for(var i = 0; i < pics.length; i++)
{
	if(window.getComputedStyle(pics[i]) && window.getComputedStyle(pics[i]).width != "auto" && pics[i].width > 40 && pics[i].height > 15)
	{
		if(pics[i].complete && pics[i].naturalWidth == 0) addSize(pics[i]);
		pics[i].addEventListener("load", e => {
			if(getDivSign(e.target))
			{
				var divToRemove = getDivSign(e.target);
				divToRemove.parentNode.removeChild(divToRemove);
				e.target.className = e.target.className.substring(0,e.target.className.length - 6);
			}
		}, true);
		pics[i].addEventListener("error", e => { addSize(e.target); }, true);
	}
}

function randomString()
{
	var string = "";
	for(var i = 0; i < 5; i++)
	{
		string += String.fromCharCode(Math.random() * 26 + 97);
	}
	return string;
}

function getDivSign(image)
{
	for(var c = 0; c < image.classList.length; c++)
	{
		var possibleDiv = image.parentNode.querySelector("div." + image.classList[c]);
		if(possibleDiv) return possibleDiv;
	}
	return null;
}

function getOffset(element)
{
	var elementCoords = element.getBoundingClientRect();
	var positioningParent = getPositioningElement(element);
	if(positioningParent != null)
	{
		var parentCoords = positioningParent.getBoundingClientRect();
		var left = elementCoords.left - parentCoords.left;
		var top = elementCoords.top - parentCoords.top;
		//var left = elementCoords.left;
		//var top = elementCoords.top;
	}
	else
	{
		var left = elementCoords.left + window.scrollX;
		var top = elementCoords.top + window.scrollY;
	}
	return {left:left, top:top};
}

function getPositioningElement(start)
{
	var startingPoint = start.parentNode;
	while(startingPoint != document.body)
	{
		if(window.getComputedStyle(startingPoint).position == "relative" && window.getComputedStyle(startingPoint).position != "absolute") return startingPoint;
		startingPoint = startingPoint.parentNode;
	}
	return null;
}

var observer = new MutationObserver(function(mutations) {
	mutations.forEach(function(mutation) {
		var sizeDivs = document.querySelectorAll(".imageSize");
		observer.disconnect();
		Array.prototype.forEach.call(sizeDivs,
			div => {
				var offset = getOffset(document.querySelector("img." + div.classList[1]));
				if(div.style.left != Math.round(offset.left) + "px") div.style.left = Math.round(offset.left) + "px";
				if(div.style.top != Math.round(offset.top) +  "px") div.style.top = Math.round(offset.top) + "px";
			}
		);
		observer.observe(document.body, config);
		if(mutation.target.nodeName == "IMG" && mutation.attributeName == "src" && getDivSign(mutation.target))
		{
			var divToRemove = getDivSign(mutation.target);
			divToRemove.parentNode.removeChild(divToRemove);
			mutation.target.className = mutation.target.className.substring(0,mutation.target.className.length - 6);
		}
	});
});

var config = { attributes: true, subtree: true, childList: true, attributeOldValue: true };
observer.observe(document.body, config);