Image Map Fixer

Finds image maps where the image fails to load and add colored blocks to make areas more visible.

目前為 2015-12-29 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name          Image Map Fixer
// @namespace     DoomTay
// @description   Finds image maps where the image fails to load and add colored blocks to make areas more visible.
// @version       0.8.0
// @grant         GM_xmlhttpRequest

// ==/UserScript==

var pics = document.body.querySelectorAll("img[usemap]");

for(var i = 0; i < pics.length; i++)
{
	evaluateImage(pics[i]);
}

function evaluateImage(pic)
{
	GM_xmlhttpRequest({
		url: pic.src,
		method: "HEAD",
		onload: function(response) {
			//Going off of response code is unreliable. Sometimes an image will return a status code of 200 even though it would redirect to an error page should you view the image directly, so we're looking at content type instead
			if(response.responseHeaders.indexOf("Content-Type: text/html") > -1)
			{
				var dimensions = {x:pic.getBoundingClientRect().x, y: pic.getBoundingClientRect().y};
				var map = document.body.querySelector("[name = " + pic.getAttribute("usemap").substring(1) + "]");
				for(a = 0; a < map.areas.length; a++)
				{
					switch(map.areas[a].getAttribute("shape"))
					{
						case "RECT":
							var parsedCoords = /(\d+), (\d+), (\d+), (\d+)/.exec(map.areas[a].getAttribute("coords"));
							var coords = {x: parseInt(parsedCoords[1]), y: parseInt(parsedCoords[2])};
							coords.width = parseInt(parsedCoords[3]) - coords.x;
							coords.height = parseInt(parsedCoords[4]) - coords.y;
							var areaLink = document.createElement("A");    
							areaLink.href = map.areas[a].href;
							areaLink.style.position = "absolute";
							areaLink.style.left = dimensions.x + coords.x;
							areaLink.style.top = dimensions.y + coords.y;
							areaLink.style.backgroundColor = randomColor();
							areaLink.style.width = coords.width;
							areaLink.style.height = coords.height;
							areaLink.style.overflow = "auto";
							pic.parentNode.insertBefore(areaLink, pic.nextSibling); 
							break;
						default:
							break;
					}
					
				}
			}
		}
	});
}

function randomColor() {
	var color = "rgb("
	var colorValues = [];
	for(var c = 0; c < 3; c++)
	{
		colorValues.push(Math.floor(Math.random()*256));
	}
	color += colorValues + ")";
	return color;
}