// ==UserScript==
// @name SE Preview on hover
// @description Shows preview of the linked questions/answers on hover while Ctrl key is held
// @version 0.0.3
// @author wOxxOm
// @namespace wOxxOm.scripts
// @license MIT License
// @match *://*.stackoverflow.com/*
// @match *://*.superuser.com/*
// @match *://*.serverfault.com/*
// @match *://*.askubuntu.com/*
// @match *://*.stackapps.com/*
// @match *://*.mathoverflow.net/*
// @match *://*.stackexchange.com/*
// @require https://greasyfork.org/scripts/12228/code/setMutationHandler.js
// @grant GM_addStyle
// @grant GM_xmlhttpRequest
// @connect stackoverflow.com
// @connect superuser.com
// @connect serverfault.com
// @connect askubuntu.com
// @connect stackapps.com
// @connect mathoverflow.net
// @connect stackexchange.com
// @connect cdn.sstatic.net
// @run-at document-end
// @noframes
// @downloadURL none
// ==/UserScript==
/* jshint lastsemic:true, multistr:true, laxbreak:true, -W030, -W041, -W084 */
const PREVIEW_DELAY = 100;
const COLORS = {
question: {
back: '80, 133, 195',
fore: '#265184',
},
answer: {
back: '112, 195, 80',
fore: '#3f7722',
},
};
var xhr;
var preview;
var previewLink;
var previewTimer;
var previewCSScache = {};
var hovering = {stoppedAt: {x:0, y:0}};
const rx = getURLregexForMatchedSites();
const thisPageBaseUrl = (location.href.match(rx) || [])[0];
const thisPageBaseUrlShort = thisPageBaseUrl ? thisPageBaseUrl.replace('/questions/', '/q/') : undefined;
const stylesOverride = ``;
GM_addStyle(`
#SEpreview {
all: unset;
box-sizing: content-box;
width: 720px; /* 660px + 30px + 30px */
height: 33%;
min-height: 200px;
position: fixed;
transition: opacity .25s ease-in-out;
right: 0;
bottom: 0;
padding: 0;
margin: 0;
background: white;
box-shadow: 0 0 100px rgba(0,0,0,0.5);
z-index: 999999;
border: 8px solid rgb(${COLORS.question.back});
}
#SEpreview.SEpreviewIsAnswer {
border-color: rgb(${COLORS.answer.back});
}
`);
processExistingAndSetMutationHandler('a', onLinkAdded);
/**************************************************************/
function onLinkAdded(links) {
for (var i = 0, link; (link = links[i++]); ) {
if (rx.test(link.href) &&
!link.matches('.short-link') &&
!link.href.startsWith(thisPageBaseUrl) &&
!link.href.startsWith(thisPageBaseUrlShort)
) {
link.removeAttribute('title');
link.addEventListener('mouseover', onLinkHovered);
}
}
}
function onLinkHovered(e) {
if (e.ctrlKey || e.altKey || e.shiftKey || e.metaKey)
return;
previewLink = this;
previewLink.addEventListener('mousemove', onLinkMouseMove);
previewLink.addEventListener('mouseout', abortPreview);
previewLink.addEventListener('mousedown', abortPreview);
restartPreviewTimer();
}
function onLinkMouseMove(e) {
if (Math.abs(hovering.stoppedAt.x - e.clientX) < 2 && Math.abs(hovering.stoppedAt.y - e.clientY) < 2)
return;
hovering.stoppedAt.x = e.clientX;
hovering.stoppedAt.y = e.clientY;
restartPreviewTimer();
}
function restartPreviewTimer() {
clearTimeout(previewTimer);
previewTimer = setTimeout(() => {
previewTimer = 0;
if (!previewLink.matches(':hover'))
return;
downloadPage();
}, PREVIEW_DELAY);
}
function abortPreview() {
previewLink.removeEventListener('mousemove', onLinkMouseMove);
previewLink.removeEventListener('mouseout', abortPreview);
previewLink.removeEventListener('mousedown', abortPreview);
clearTimeout(previewTimer);
previewTimer = setTimeout(() => {
previewTimer = 0;
if (preview && !preview.matches(':hover'))
hidePreview();
}, 500);
if (xhr)
xhr.abort();
}
function downloadPage() {
xhr = GM_xmlhttpRequest({
method: 'GET',
url: previewLink.href,
onload: showPreview,
});
}
function showPreview(data) {
var doc = new DOMParser().parseFromString(data.responseText, 'text/html');
if (!doc || !doc.head) {
console.error(GM_info.script.name, 'empty document received:', data);
return;
}
if (!$(doc, 'base'))
doc.head.insertAdjacentHTML('afterbegin', `