Created
February 23, 2015 13:37
-
-
Save malko/ea530a66b5927ea03d5c to your computer and use it in GitHub Desktop.
Confluence fancybox enhance navigation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Confluence fancybox enhance navigation | |
// @version 0.0.1 | |
// @description add navigation through image previews | |
// @author Jonathan Gotti | |
// @match http://*/* | |
// @match https://*/* | |
// ==/UserScript== | |
// update match rules for your domain | |
(function(){ | |
var slice = function(a) { return [].slice.call(a); }, | |
style = document.createElement('style'), | |
images = slice(document.querySelectorAll('img.confluence-embedded-image')), | |
left = document.querySelector('a#fancybox-left'), | |
right = document.querySelector('a#fancybox-right'), | |
srcPrefix = window.location.origin | |
; | |
//left.style.display = right.style.display = 'block'; | |
style.innerHTML = "#fancybox-left, #fancybox-right{ display:block !important;} img.current{ outline:solid red 1px;}"; | |
document.body.appendChild(style); | |
images = images.map(function(elmt) { | |
return [elmt, srcPrefix + elmt.dataset.imageSrc]; | |
}); | |
function resetCurrentClass() { | |
images.forEach(function(item){ item[0].classList.remove('current'); }); | |
} | |
function getPos() { | |
var src = document.querySelector('img#fancybox-img').src, | |
pos = -1 | |
; | |
images.some(function(item, key){ | |
if (item[1] === src) { | |
pos = key; | |
return true; | |
} | |
}); | |
return pos; | |
} | |
function goTo(pos) { | |
resetCurrentClass(); | |
images[pos][0].click(); | |
images[pos][0].classList.add('current'); | |
} | |
function next(){ | |
var pos = getPos() + 1; | |
(pos >= images.length) && (pos = 0); | |
goTo(pos); | |
} | |
function prev(){ | |
var pos = getPos() - 1; | |
(pos < 0) && (pos = images.length - 1); | |
goTo(pos); | |
} | |
left.addEventListener('click', prev); | |
right.addEventListener('click', next); | |
document.body.addEventListener('keydown', function(evt){ | |
switch(evt.keyIdentifier) { | |
case 'U+001B': | |
resetCurrentClass(); break; | |
case 'Left': | |
prev(); break; | |
case 'Right': | |
next(); break; | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment