Участник:Oblitus/WikiSup
Материал из Lurkmore
Этот скрипт позволяет просматривать сноски без перехода по ссылкам. Имеет три варианта работы: по mouseover, по клику и комбинированный, при котором клик фиксирует состояние.
// ==UserScript== // @name Wiki Sup Extension // @description Add tooltip when click on ref // @include *wiki* // @include http://lurkmore.ru/* // ==/UserScript== var showOnMouseOver = "both"; // Change here true/false/both addEvent (window, "load", modifyRefs); var divContent = null; var divTooltip = document.createElement("DIV"); with (divTooltip.style) { width = "20em"; fontWeight = "normal"; fontStyle = "normal"; fontSize = "9pt"; borderStyle = "solid"; borderWidth = "1px 2px 2px 1px"; borderColor = "#777777"; backgroundColor = "#FFF8C0"; padding = "1pt 3pt"; position = "absolute"; display = "none"; } function modifyRefs () { divContent = document.getElementById ("content"); divContent.appendChild (divTooltip); var sups = document.getElementsByTagName ("sup"); for (var i=0; i < sups.length; i++) { if (sups[i].className == "reference") { var supLink = sups[i].firstChild.getAttribute ("href"); supLink = supLink.substring (supLink.indexOf("#")+1); var tipText = cropArrow (document.getElementById(supLink)); addTooltipHook (sups[i], tipText); } } } function cropArrow (elem) { var oldChild = elem.removeChild (elem.firstChild); var retText = elem.innerHTML; elem.insertBefore (oldChild, elem.firstChild); return retText; } var currentTip = null; var fix = false; function addTooltipHook (elem, tipText) { var tooltipFunction = function (event) { if (elem == currentTip) { if(divTooltip.style.display == "none") { divTooltip.style.display = "inline"; } else if(event.type == "click" && showOnMouseOver == "both") { fix = !fix; } else if(fix == false) { divTooltip.style.display = "none";} } else { divTooltip.style.display = "inline"; divTooltip.innerHTML = tipText; var coords = getCoords(elem); var offset = getCoords(divContent); var scrWidth = divContent.offsetWidth; var tipWidth = divTooltip.offsetWidth; var new_x = coords.x - offset.x + 20; var new_y = coords.y - offset.y + 10; if (new_x + tipWidth > scrWidth) {new_x -= tipWidth + 20}; divTooltip.style.left = new_x + "px"; divTooltip.style.top = new_y + "px"; currentTip = elem; } if (elem.addEventListener) { event.stopPropagation(); event.preventDefault(); } return false; } if (showOnMouseOver=="true"||showOnMouseOver=="both") { addEvent (elem, "mouseover", tooltipFunction); addEvent (elem, "mouseout", tooltipFunction); } if (showOnMouseOver=="false"||showOnMouseOver=="both") { addEvent (elem, "click", tooltipFunction); } } function getCoords (elem) { var ret = {x:0, y:0}; do { ret.x += elem.offsetLeft; ret.y += elem.offsetTop; elem = elem.offsetParent; } while (elem != null); return ret; } function addEvent (victim, event, func) { if (victim.addEventListener) {victim.addEventListener (event, func, false)}; if (victim.attachEvent) {victim.attachEvent ("on"+event, func)}; }