2021-01-21 13:06:12 +00:00
|
|
|
// ==UserScript==
|
|
|
|
// @name DuckDuckGo Images Slideshow
|
|
|
|
// @namespace https://bullercodeworks.com
|
2021-02-19 15:50:29 +00:00
|
|
|
// @version 0.3
|
2021-01-21 13:06:12 +00:00
|
|
|
// @description Takes a DuckDuckGo Images page and turns it into a slideshow.
|
2021-02-19 15:50:29 +00:00
|
|
|
// @author brian@bullercodeworks.com
|
2021-01-21 13:06:12 +00:00
|
|
|
// @match https://duckduckgo.com/*
|
|
|
|
// @grant none
|
|
|
|
// ==/UserScript==
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
'use strict';
|
2021-01-21 16:55:54 +00:00
|
|
|
|
|
|
|
var init = function() {
|
|
|
|
lb.style.textAlign = 'center';
|
|
|
|
lb.style.height = '100%';
|
|
|
|
lb.style.width = '100%';
|
|
|
|
lb.style.position = 'fixed';
|
|
|
|
lb.style.top = '0px';
|
|
|
|
lb.style.left = '0px';
|
|
|
|
lb.style.zIndex = '99999';
|
|
|
|
lb.style.backgroundColor = 'rgba(0, 0, 0, 0.85)';
|
|
|
|
document.body.prepend(lb);
|
|
|
|
lbimg.style.objectFit = 'cover';
|
|
|
|
lb.append(lbimg);
|
2021-02-19 15:50:29 +00:00
|
|
|
imgCounter.style.backgroundColor = 'rgba(0, 0, 0, 0.85)';
|
|
|
|
imgCounter.style.color = 'white';
|
|
|
|
imgCounter.style.position = 'fixed';
|
|
|
|
imgCounter.style.top = '0px';
|
|
|
|
imgCounter.style.left = '0px';
|
|
|
|
lb.append(imgCounter);
|
2021-01-21 16:55:54 +00:00
|
|
|
running = true;
|
|
|
|
showImage(0);
|
|
|
|
}
|
|
|
|
|
2021-01-21 13:38:31 +00:00
|
|
|
var addSlideshowButton = function() {
|
|
|
|
if(document.getElementById('slideshow_button') == null) {
|
|
|
|
var slideshowLi = document.createElement('li');
|
|
|
|
var slideshowA = document.createElement('a');
|
|
|
|
slideshowLi.id = "slideshow_button";
|
|
|
|
slideshowLi.classList.add('zcm__item');
|
|
|
|
slideshowA.classList.add('zcm__link');
|
|
|
|
slideshowA.classList.add('js-zci-link');
|
|
|
|
slideshowA.innerText="Slideshow";
|
|
|
|
slideshowA.href='#';
|
|
|
|
slideshowLi.appendChild(slideshowA);
|
|
|
|
slideshowA.onclick = init;
|
|
|
|
document.getElementById('duckbar_static').appendChild(slideshowLi);
|
2021-01-21 13:06:12 +00:00
|
|
|
}
|
2021-01-21 13:38:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var currimg=0;
|
|
|
|
var allimages = document.querySelectorAll('img');
|
|
|
|
var timer;
|
|
|
|
var imageTime = 3000;
|
|
|
|
var running = false;
|
|
|
|
var paused = false;
|
|
|
|
var imgcount=allimages.length;
|
|
|
|
var lb = document.createElement('div');
|
|
|
|
var lbimg = document.createElement('img');
|
2021-02-19 15:50:29 +00:00
|
|
|
var imgCounter = document.createElement('span');
|
2021-01-21 13:38:31 +00:00
|
|
|
var lastScroll = 0;
|
|
|
|
|
|
|
|
|
|
|
|
var tab = document.querySelector('.js-zci-link--images');
|
|
|
|
if(tab == null) { return; } // Need that tab
|
|
|
|
if(tab.classList.contains('is-active')) { addSlideshowButton(); }
|
|
|
|
tab.addEventListener('click', addSlideshowButton);
|
|
|
|
|
2021-01-21 13:06:12 +00:00
|
|
|
function updateImages() {
|
|
|
|
var testimages = document.querySelectorAll('img');
|
|
|
|
var testcount = testimages.length;
|
|
|
|
if(testcount > imgcount) {
|
2021-02-19 15:50:29 +00:00
|
|
|
console.log("Loading more images...");
|
2021-01-21 13:06:12 +00:00
|
|
|
imgcount = testcount;
|
|
|
|
allimages = testimages;
|
|
|
|
}
|
|
|
|
}
|
2021-01-21 13:38:31 +00:00
|
|
|
|
|
|
|
|
2021-01-21 13:06:12 +00:00
|
|
|
function showImage(idx){
|
|
|
|
if(paused){
|
|
|
|
setTimeout(()=>showImage(idx),500);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
updateImages();
|
|
|
|
currimg = idx;
|
|
|
|
if(typeof allimages[idx] == 'undefined' || allimages[idx].src == "") {
|
|
|
|
timer = setTimeout(() => showImage(++idx), 100);
|
|
|
|
return;
|
|
|
|
}
|
2021-02-19 15:50:29 +00:00
|
|
|
allimages[idx].click();
|
2021-01-21 13:06:12 +00:00
|
|
|
allimages[idx].scrollIntoView();
|
2021-02-19 15:50:29 +00:00
|
|
|
console.log("Showing Image "+idx+" ("+allimages[idx]+")");
|
2021-01-21 13:06:12 +00:00
|
|
|
var useHeight=allimages[idx].height > allimages[idx].width;
|
|
|
|
if(useHeight){
|
|
|
|
lbimg.style.height = '100%';
|
|
|
|
lbimg.style.width = '';
|
|
|
|
} else {
|
|
|
|
lbimg.style.height = '';
|
|
|
|
lbimg.style.width = '100%'
|
|
|
|
}
|
|
|
|
lbimg.src = allimages[idx].src;
|
2021-02-19 15:50:29 +00:00
|
|
|
imgCounter.innerText = "Image " + idx + " / " + imgcount;
|
2021-01-21 13:38:31 +00:00
|
|
|
timer = setTimeout(() => showImage(++idx), imageTime);
|
2021-01-21 13:06:12 +00:00
|
|
|
}
|
2021-01-21 13:38:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
function promptTime() {
|
|
|
|
clearTimeout(timer);
|
|
|
|
var resp = parseFloat(prompt("Image Time (in seconds):", imageTime/1000));
|
|
|
|
imageTime = resp * 1000;
|
|
|
|
showImage(currimg);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-21 13:06:12 +00:00
|
|
|
function goto(){
|
|
|
|
clearTimeout(timer);
|
|
|
|
var resp = parseInt(prompt("GOTO:", currimg));
|
|
|
|
if(resp != NaN) {
|
|
|
|
doGoto(resp)
|
|
|
|
} else {
|
|
|
|
alert("Not a Number: "+resp)
|
|
|
|
}
|
|
|
|
}
|
2021-01-21 13:38:31 +00:00
|
|
|
|
|
|
|
|
2021-01-21 13:06:12 +00:00
|
|
|
function doGoto(num) {
|
|
|
|
updateImages();
|
|
|
|
if(num > imgcount) {
|
|
|
|
if(window.scrollY == lastScroll) {
|
|
|
|
alert("Looks like there aren't that many images.");
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
window.scrollTo(0,document.body.scrollHeight);
|
|
|
|
lastScroll = window.scrollY;
|
|
|
|
setTimeout(()=>doGoto(num), 2000)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(num != NaN) {
|
|
|
|
showImage(num);
|
|
|
|
}
|
|
|
|
}
|
2021-01-21 13:38:31 +00:00
|
|
|
|
|
|
|
|
2021-01-21 13:06:12 +00:00
|
|
|
function pause(){
|
|
|
|
if(paused){
|
2021-02-19 15:50:29 +00:00
|
|
|
console.log("Resuming Slideshow");
|
2021-01-21 13:06:12 +00:00
|
|
|
showImage(currimg);
|
|
|
|
} else {
|
2021-02-19 15:50:29 +00:00
|
|
|
console.log("Pausing Slideshow");
|
2021-01-21 13:06:12 +00:00
|
|
|
clearTimeout(timer);
|
|
|
|
}
|
|
|
|
paused = !paused;
|
|
|
|
}
|
2021-01-21 13:38:31 +00:00
|
|
|
|
|
|
|
|
2021-01-21 13:06:12 +00:00
|
|
|
function rewind() {
|
2021-02-19 15:50:29 +00:00
|
|
|
console.log("Rewinding Slideshow");
|
2021-01-21 13:06:12 +00:00
|
|
|
clearTimeout(timer);
|
|
|
|
currimg = currimg-1;
|
|
|
|
showImage(currimg);
|
|
|
|
}
|
2021-01-21 13:38:31 +00:00
|
|
|
|
|
|
|
|
2021-01-21 13:06:12 +00:00
|
|
|
function fastforward() {
|
2021-02-19 15:50:29 +00:00
|
|
|
console.log("Fast Forwarding Slideshow");
|
2021-01-21 13:06:12 +00:00
|
|
|
clearTimeout(timer);
|
|
|
|
currimg = currimg+1;
|
|
|
|
showImage(currimg);
|
|
|
|
}
|
2021-01-21 13:38:31 +00:00
|
|
|
|
2021-02-18 16:53:22 +00:00
|
|
|
document.addEventListener('keydown',(e)=>{
|
2021-01-21 13:06:12 +00:00
|
|
|
if(!running) { return; }
|
2021-02-18 16:53:22 +00:00
|
|
|
var stopProp = false;
|
2021-01-21 13:06:12 +00:00
|
|
|
switch(e.code) {
|
|
|
|
case 'Space':
|
|
|
|
pause();
|
2021-02-18 16:53:22 +00:00
|
|
|
stopProp = true;
|
|
|
|
break;
|
|
|
|
case 'KeyH':
|
2021-01-21 13:06:12 +00:00
|
|
|
rewind();
|
2021-02-18 16:53:22 +00:00
|
|
|
stopProp = true;
|
|
|
|
break;
|
|
|
|
case 'KeyL':
|
2021-01-21 13:06:12 +00:00
|
|
|
fastforward();
|
2021-02-18 16:53:22 +00:00
|
|
|
stopProp = true;
|
|
|
|
break;
|
2021-01-21 13:38:31 +00:00
|
|
|
case 'KeyT':
|
|
|
|
promptTime();
|
2021-02-18 16:53:22 +00:00
|
|
|
stopProp = true;
|
|
|
|
break;
|
2021-01-21 13:06:12 +00:00
|
|
|
case 'KeyG':
|
|
|
|
goto();
|
2021-02-18 16:53:22 +00:00
|
|
|
stopProp = true;
|
|
|
|
break;
|
2021-01-21 13:06:12 +00:00
|
|
|
case 'Escape':
|
|
|
|
clearTimeout(timer);
|
|
|
|
running = false;
|
|
|
|
lb.remove();
|
2021-02-18 16:53:22 +00:00
|
|
|
stopProp = true;
|
|
|
|
}
|
|
|
|
if(stopProp) {
|
|
|
|
e.stopPropagation();
|
2021-01-21 13:06:12 +00:00
|
|
|
}
|
2021-02-18 16:53:22 +00:00
|
|
|
return stopProp;
|
2021-01-21 13:06:12 +00:00
|
|
|
});
|
|
|
|
})();
|