Deviantart Iframe embedding

Allows external links named "[iframe-embed]" to replace the deviation image with an iframe of the link.

Tính đến 16-04-2016. Xem phiên bản mới nhất.

// ==UserScript==
// @name         Deviantart Iframe embedding
// @namespace    http://tampermonkey.net/
// @version      0.11
// @description  Allows external links named "[iframe-embed]" to replace the deviation image with an iframe of the link.
// @author       RSGmaker
// @match        http://*.deviantart.com/art/*
// @match        http://sta.sh/*
// @grant        none
// ==/UserScript==



(function() {
    'use strict';
	//The div tag with the main image(and the different sized variants) in it.
	var devcontent = document.getElementsByClassName("dev-view-deviation")[0];
	//The url to use for the iframe.
	var link = null;
	//The size of the main image(we apply the size to the iframe).
	var W = 0;
	var H = 0;
	//What the content div had before messing with it(when the user clicks "cancel", the innerHTML is set back to this).
	var oldHtml;
	//The journal/text content, if this isn't an art deviation.
	var journal = null;
	//The div containing the warning message, if this is a journal, it will hold the iframe instead of devcontent.
	var warningDiv = null;
	if (document.getElementsByClassName("grf-indent").length>0)
	{
		journal = document.getElementsByClassName("grf-indent")[0].childNodes[1];
	}
	//The user clicked "Load content".
	function loadiframe()
	{
		var ihtml = "<iframe sandbox='allow-forms allow-pointer-lock allow-scripts allow-same-origin' width='"+W+"' height='"+H+"' src='"+link.href.replace("http://www.deviantart.com/users/outgoing?","")+"'></iframe>";;
		if (journal == null)
		{
			devcontent.innerHTML = ihtml;
		}
		else
		{
			warningDiv.innerHTML = ihtml;
		}
	}
	//The user clicked "Cancel".
	function cancelload()
	{
		if (journal == null)
		{
			devcontent.innerHTML = oldHtml;
		}
		else
		{
			warningDiv.parentNode.replaceChild(link,warningDiv);
		}
	}
	function getTextWithinText(text,startText,endText)
	{
		var SI = text.indexOf(startText);
		var EI = text.indexOf(endText,SI+startText.length);
		if (SI>=0 && EI>SI)
		{
			return text.slice(SI+startText.length,EI);
		}
		return null;
	}
	function checkForEmbed(startCommandText,endCommandText,loadFunction)
	{
		if (devcontent)
    {
		oldHtml = devcontent.innerHTML;
		//All <a> tags within the deviation description.
		var links = null;
		if (journal != null)
		{
			links = journal.getElementsByClassName("external");
		}
		else
		{
			links = document.getElementsByClassName("text block")[0].getElementsByClassName("external");
		}
		if (!(links && links.length>0))
		{
			return;
		}
		var i = 0;
		//Search for an "[iframe-embed]" command within the description.
		while (i < links.length)
		{
			var text = links[i].innerHTML.toLowerCase();
			var ind = text.indexOf(startCommandText);
			if (ind>=0 && text.indexOf(endCommandText)>ind)
			{
				link = links[i];
				
				var tmp = getTextWithinText(text,"width=\"","\"");
				if (tmp != null)
				{
					W = tmp;
				}
				tmp = getTextWithinText(text,"height=\"","\"");
				if (tmp != null)
				{
					H = tmp;
				}
			}
			i++;
		}
        if (link == null)
		{
			//This disabled code would allow for iframe embedding of an <a> tag that is literally the very first thing of the description.(I decided that this doesn't make all that much sense.)
			/*if (oldHtml.indexOf("<a class=\"external\" href=\"http://www.deviantart.com/users/outgoing?")==0)
			{
				link = links[0].href.replace("http://www.deviantart.com/users/outgoing?","");
			}
			else*/
			{
				//No [iframe-embed] detected.
				return;
			}
		}
		
		if (W == 0 || H == 0)
		{
			//Get the size of the main image.
			var C = devcontent.childNodes[1];
			if (W == 0)
			{
				W = C.width;
			}
			if (H == 0)
			{
				H = C.height;
			}
		}
		
		//Create the load content warning box, this allows the user to review the url, and decide to allow the iframe or not.
		var DV = document.createElement("DIV");
		warningDiv = DV;
		if (journal == null)
		{
			//Make the box float over the top of the main image.
			DV.style = "position:absolute;left:35%;top:"+(H*0.3)+"px;background-color:white";
		}
		else
		{
			DV.style = "background-color:white";
		}
		var center = document.createElement("CENTER");
		center.style = "border: 1px solid black";
		var T = document.createElement("TABLE");
		var row = T.insertRow(-1);
		var col = row.insertCell(-1);
		col.innerHTML = "This deviation contains embedded content from another location, do you want to load it?<br/>(Only load content from sources you trust.)<br/>URL:"+link.href.replace("http://www.deviantart.com/users/outgoing?",""); 
		row = T.insertRow(-1);
		col = row.insertCell(-1);
		
		var center2 = document.createElement("CENTER");
		
		var Btn = document.createElement("INPUT");
		Btn.type = "button";
		Btn.value = "Load content";
		Btn.onclick = loadFunction;
		
		center2.appendChild(Btn);
		
		var spacing = document.createElement("P");
		spacing.innerHTML = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		spacing.style="display:inline";
		center2.appendChild(spacing);
		
		var Btn2 = document.createElement("INPUT");
		Btn2.type = "button";
		Btn2.value = "Cancel";
		Btn2.onclick = cancelload;
		
		center2.appendChild(Btn2);
		col.appendChild(center2);
		center.appendChild(T);
		DV.appendChild(center);
		if (journal == null)
		{
			devcontent.appendChild(DV);
		}
		else
		{
			link.parentNode.replaceChild(DV,link);
		}
    }
	}
	
    checkForEmbed("[iframe-embed","]",loadiframe);
	
})();
长期地址
遇到问题?请前往 GitHub 提 Issues。