userscripts/visual/4c-gallery.js

254 lines
5.3 KiB
JavaScript
Raw Normal View History

2023-02-16 21:20:12 +01:00
// ==UserScript==
2023-02-21 21:47:46 +01:00
// @name 4c-gallery
// @description Draggable image viewer
2023-02-16 21:20:12 +01:00
// @author 17ms
// @license MIT License
// @namespace Violentmonkey Scripts
2023-02-17 07:40:07 +01:00
// @match *://boards.4chan*.org/*/thread/*
// @exclude *://boards.4chan*.org/*/catalog
2023-04-25 13:52:24 +02:00
// @version 1.3.2
2023-02-16 21:20:12 +01:00
// ==/UserScript==
2023-04-25 13:52:24 +02:00
/*
Default shortcuts:
"-" decrease window size
"+" increase window size
"j" previous image
"k" next image
"i" jump to the source (i.e. post's hash)
"l" open the image to a new tab
*/
const keys = ["-", "+", "j", "k", "i", "l"]
const excludeWebm = true // not tested with webms enabled
2023-02-16 22:38:55 +01:00
2023-02-16 21:20:12 +01:00
const dragElement = (elem) => {
2023-02-19 21:31:30 +01:00
const handler = (e) => {
2023-02-16 22:38:55 +01:00
e = e || window.event
e.preventDefault()
pos3 = e.clientX
pos4 = e.clientY
2023-02-19 21:31:30 +01:00
document.onmouseup = closeDragger
document.onmousemove = enableDragger
2023-02-16 22:38:55 +01:00
}
2023-02-19 21:31:30 +01:00
const enableDragger = (e) => {
2023-02-16 22:38:55 +01:00
e = e || window.event
e.preventDefault()
2023-04-25 19:07:30 +02:00
2023-02-16 22:38:55 +01:00
pos1 = pos3 - e.clientX
pos2 = pos4 - e.clientY
pos3 = e.clientX
pos4 = e.clientY
2023-04-25 19:07:30 +02:00
elem.style.top = elem.offsetTop - pos2 + "px"
elem.style.left = elem.offsetLeft - pos1 + "px"
2023-02-16 22:38:55 +01:00
}
2023-02-19 21:31:30 +01:00
const closeDragger = () => {
2023-02-16 22:38:55 +01:00
document.onmouseup = null
document.onmousemove = null
}
let pos1, pos2, pos3, pos4
if (document.getElementsByClassName("drDrag").length > 0) {
2023-02-19 21:31:30 +01:00
document.getElementsByClassName("drDrag")[0].onmousedown = handler
2023-02-16 22:38:55 +01:00
} else {
2023-02-19 21:31:30 +01:00
elem.onmousedown = handler
2023-02-16 22:38:55 +01:00
}
2023-02-16 21:20:12 +01:00
}
const prevImg = () => {
2023-02-16 22:38:55 +01:00
if (i === 0) {
i = imgs.length - 1
2023-02-16 22:38:55 +01:00
} else {
i--
}
2023-02-16 21:20:12 +01:00
imgElem.src = imgs[i].src
2023-02-16 21:20:12 +01:00
}
const nextImg = () => {
if (i === imgs.length - 1) {
2023-02-16 22:38:55 +01:00
i = 0
} else {
i++
}
2023-02-16 21:20:12 +01:00
imgElem.src = imgs[i].src
2023-02-16 21:20:12 +01:00
}
2023-02-19 21:31:30 +01:00
const sizeUp = () => {
const newW = baseElem.clientWidth + 200
const newH = Math.round(newW * ratioMultiplier)
2023-02-19 21:31:30 +01:00
if (newW > window.innerWidth) {
return
2023-02-19 21:31:30 +01:00
}
baseElem.style.width = newW + "px"
baseElem.style.height = newH + "px"
2023-02-19 21:31:30 +01:00
}
const sizeDown = () => {
const newW = baseElem.clientWidth - 200
const newH = Math.round(newW * ratioMultiplier)
2023-02-19 21:31:30 +01:00
if (newW < 265 || newH < 240) {
return
2023-02-19 21:31:30 +01:00
}
baseElem.style.width = newW + "px"
baseElem.style.height = newH + "px"
2023-02-19 21:31:30 +01:00
}
const moveToHash = () => {
const hash = sources[i][1]
const url = window.location.href.split("#")[0]
window.location.href = url + hash
}
2023-02-19 21:31:30 +01:00
2023-04-25 13:52:24 +02:00
const openToNew = () => {
const url = sources[i][0]
window.open(url, "_blank")
}
const preloadImgs = async () => {
for (let s of sources) {
let img = new Image()
img.src = s[0]
await img.decode()
imgs.push(img)
2023-02-19 21:31:30 +01:00
}
}
2023-02-19 21:31:30 +01:00
const keyUpEvent = async (e) => {
if (["input", "textarea"].includes(e.target.tagName.toLowerCase())) {
return
}
2023-04-25 13:52:24 +02:00
switch (e.key) {
case keys[0]:
sizeDown()
break
case keys[1]:
sizeUp()
break
case keys[2]:
prevImg()
break
case keys[3]:
nextImg()
break
case keys[4]:
moveToHash()
break
case keys[5]:
openToNew()
break
2023-02-19 21:31:30 +01:00
}
}
const createElements = () => {
const limitElem = document.getElementsByClassName("boardBanner")[0]
const newNode = document.createElement("div")
newNode.innerHTML = `<div id="drGallery" class="reply drDrag drag">
<div id="drHeader">Gallery</div>
2023-02-19 21:31:30 +01:00
<div id="drContainer"><img id="drImg" src="" alt="" /></div>
2023-02-16 21:20:12 +01:00
</div>`
const initW = "265px"
const initH = Math.round(265 * ratioMultiplier) + "px"
const stylesheet = document.createElement("style")
stylesheet.innerText = `#drGallery {
2023-02-16 21:20:12 +01:00
position: absolute;
z-index: 9;
2023-02-19 21:31:30 +01:00
font-weight: bold;
2023-02-16 21:20:12 +01:00
text-align: center;
2023-02-19 21:31:30 +01:00
border: 1px solid black;
width: ${initW};
height: ${initH};
min-width: ${initW};
min-height: ${initH};
display: none;
flex-direction: column;
2023-02-16 21:20:12 +01:00
}
#drHeader {
2023-02-19 21:31:30 +01:00
min-height: max-content;
2023-02-16 21:20:12 +01:00
padding: 5px;
}
#drContainer {
2023-02-19 21:31:30 +01:00
width: inherit;
height: 100%;
2023-02-19 21:31:30 +01:00
position: relative;
2023-02-16 21:20:12 +01:00
}
#drContainer img {
2023-02-19 21:31:30 +01:00
max-width: 100%;
max-height: 98%;
2023-02-19 21:31:30 +01:00
width: auto;
height: auto;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
2023-02-16 21:20:12 +01:00
}`
document.head.appendChild(stylesheet)
document.body.insertBefore(newNode, limitElem)
2023-04-25 19:07:30 +02:00
document.getElementsByClassName("navLinks desktop")[0].innerHTML +=
" [<a href='javascript:toggleGalleryVisibility()'>Gallery</a>]"
}
2023-02-16 21:20:12 +01:00
const collectSources = () => {
let sources = []
const fileDivs = document.getElementsByClassName("fileThumb")
2023-04-25 19:07:30 +02:00
const hashPrefix = document
.getElementsByClassName("postNum")[0]
.children[0].hash.slice(0, 3)
for (let e of fileDivs) {
const s = e.href.split(".")
const filetype = s[s.length - 1]
2023-02-16 21:20:12 +01:00
if (excludeWebm && filetype === "webm") {
continue
}
// div's id to post's hash (prefix x): fT12345678 => #px12345678
sources.push([e.href, hashPrefix + e.parentElement.id.slice(2)])
}
return sources
2023-02-16 21:20:12 +01:00
}
window.toggleGalleryVisibility = () => {
if (baseElem.style.display === "flex") {
baseElem.style.display = "none"
document.removeEventListener("keyup", keyUpEvent, false)
} else {
baseElem.style.display = "flex"
document.addEventListener("keyup", keyUpEvent, false)
}
}
const ratioMultiplier = window.innerHeight / window.innerWidth
createElements()
let i = 0
let imgs = []
const sources = collectSources()
preloadImgs().then(() => console.log("4c-gallery: All images loaded"))
const baseElem = document.getElementById("drGallery")
const imgElem = document.getElementById("drImg")
dragElement(baseElem)