eslint config

This commit is contained in:
17ms 2023-06-01 17:55:54 +03:00
parent 9b2f9d800b
commit f0ea331f57
6 changed files with 175 additions and 160 deletions

15
.eslintrc.json Normal file
View File

@ -0,0 +1,15 @@
{
"env": {
"browser": true
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 2021
},
"rules": {
"quotes": ["warn", "double"],
"semi": ["warn", "never"],
"indent": ["warn", 4],
"prettier/prettier": 0
}
}

View File

@ -9,30 +9,30 @@
// ==/UserScript== // ==/UserScript==
function download(url) { function download(url) {
fetch(url, { fetch(url, {
mode: "no-cors" mode: "no-cors",
})
.then((response) => response.blob())
.then((blob) => {
let blob_url = window.URL.createObjectURL(blob)
let a = document.createElement("a")
a.download = url.split("/")[5]
a.href = blob_url
document.body.appendChild(a)
a.click()
a.remove()
}) })
.then((response) => response.blob())
.then((blob) => {
let blob_url = window.URL.createObjectURL(blob)
let a = document.createElement("a")
a.download = url.split("/")[5]
a.href = blob_url
document.body.appendChild(a)
a.click()
a.remove()
})
} }
function init() { function init() {
const links = Array.from(document.getElementsByClassName("jpg")).concat( const links = Array.from(document.getElementsByClassName("jpg")).concat(
Array.from(document.getElementsByClassName("png")) Array.from(document.getElementsByClassName("png"))
) )
for (let i = 0; i < links.length; ++i) { for (let i = 0; i < links.length; ++i) {
let url = links[i].href let url = links[i].href
download(url) download(url)
} }
} }
const activate_link = document.createElement("button") const activate_link = document.createElement("button")

View File

@ -9,15 +9,15 @@
// ==/UserScript== // ==/UserScript==
window.addEventListener("load", () => { window.addEventListener("load", () => {
const keywords = [] // e.g. ["/sdg/", "luke smith"] const keywords = [] // e.g. ["/sdg/", "luke smith"]
const data = document.getElementsByClassName("teaser") const data = document.getElementsByClassName("teaser")
for (let i = 0; i < data.length; ++i) { for (let i = 0; i < data.length; ++i) {
let innerText = data[i].innerText let innerText = data[i].innerText
for (let j = 0; j < keywords.length; ++j) { for (let j = 0; j < keywords.length; ++j) {
if (innerText.includes(keywords[j]) && data[i].offsetParent !== null) { if (innerText.includes(keywords[j]) && data[i].offsetParent !== null) {
data[i].offsetParent.style.display = "none" data[i].offsetParent.style.display = "none"
} }
}
} }
}
}) })

View File

@ -23,147 +23,147 @@ const keys = ["-", "+", "j", "k", "i", "l"]
const excludeWebm = true // not tested with webms enabled const excludeWebm = true // not tested with webms enabled
const dragElement = (elem) => { const dragElement = (elem) => {
const handler = (e) => { const handler = (e) => {
e = e || window.event e = e || window.event
e.preventDefault() e.preventDefault()
pos3 = e.clientX pos3 = e.clientX
pos4 = e.clientY pos4 = e.clientY
document.onmouseup = closeDragger document.onmouseup = closeDragger
document.onmousemove = enableDragger document.onmousemove = enableDragger
} }
const enableDragger = (e) => { const enableDragger = (e) => {
e = e || window.event e = e || window.event
e.preventDefault() e.preventDefault()
pos1 = pos3 - e.clientX pos1 = pos3 - e.clientX
pos2 = pos4 - e.clientY pos2 = pos4 - e.clientY
pos3 = e.clientX pos3 = e.clientX
pos4 = e.clientY pos4 = e.clientY
elem.style.top = elem.offsetTop - pos2 + "px" elem.style.top = elem.offsetTop - pos2 + "px"
elem.style.left = elem.offsetLeft - pos1 + "px" elem.style.left = elem.offsetLeft - pos1 + "px"
} }
const closeDragger = () => { const closeDragger = () => {
document.onmouseup = null document.onmouseup = null
document.onmousemove = null document.onmousemove = null
} }
let pos1, pos2, pos3, pos4 let pos1, pos2, pos3, pos4
if (document.getElementsByClassName("drDrag").length > 0) { if (document.getElementsByClassName("drDrag").length > 0) {
document.getElementsByClassName("drDrag")[0].onmousedown = handler document.getElementsByClassName("drDrag")[0].onmousedown = handler
} else { } else {
elem.onmousedown = handler elem.onmousedown = handler
} }
} }
const prevImg = () => { const prevImg = () => {
if (i === 0) { if (i === 0) {
i = imgs.length - 1 i = imgs.length - 1
} else { } else {
i-- i--
} }
imgElem.src = imgs[i].src imgElem.src = imgs[i].src
} }
const nextImg = () => { const nextImg = () => {
if (i === imgs.length - 1) { if (i === imgs.length - 1) {
i = 0 i = 0
} else { } else {
i++ i++
} }
imgElem.src = imgs[i].src imgElem.src = imgs[i].src
} }
const sizeUp = () => { const sizeUp = () => {
const newW = baseElem.clientWidth + 200 const newW = baseElem.clientWidth + 200
const newH = Math.round(newW * ratioMultiplier) const newH = Math.round(newW * ratioMultiplier)
if (newW > window.innerWidth) { if (newW > window.innerWidth) {
return return
} }
baseElem.style.width = newW + "px" baseElem.style.width = newW + "px"
baseElem.style.height = newH + "px" baseElem.style.height = newH + "px"
} }
const sizeDown = () => { const sizeDown = () => {
const newW = baseElem.clientWidth - 200 const newW = baseElem.clientWidth - 200
const newH = Math.round(newW * ratioMultiplier) const newH = Math.round(newW * ratioMultiplier)
if (newW < 265 || newH < 240) { if (newW < 265 || newH < 240) {
return return
} }
baseElem.style.width = newW + "px" baseElem.style.width = newW + "px"
baseElem.style.height = newH + "px" baseElem.style.height = newH + "px"
} }
const moveToHash = () => { const moveToHash = () => {
const hash = sources[i][1] const hash = sources[i][1]
const url = window.location.href.split("#")[0] const url = window.location.href.split("#")[0]
window.location.href = url + hash window.location.href = url + hash
} }
const openToNew = () => { const openToNew = () => {
const url = sources[i][0] const url = sources[i][0]
window.open(url, "_blank") window.open(url, "_blank")
} }
const preloadImgs = async () => { const preloadImgs = async () => {
for (let s of sources) { for (let s of sources) {
let img = new Image() let img = new Image()
img.src = s[0] img.src = s[0]
await img.decode() await img.decode()
imgs.push(img) imgs.push(img)
} }
} }
const keyUpEvent = async (e) => { const keyUpEvent = async (e) => {
if (["input", "textarea"].includes(e.target.tagName.toLowerCase())) { if (["input", "textarea"].includes(e.target.tagName.toLowerCase())) {
return return
} }
switch (e.key) { switch (e.key) {
case keys[0]: case keys[0]:
sizeDown() sizeDown()
break break
case keys[1]: case keys[1]:
sizeUp() sizeUp()
break break
case keys[2]: case keys[2]:
prevImg() prevImg()
break break
case keys[3]: case keys[3]:
nextImg() nextImg()
break break
case keys[4]: case keys[4]:
moveToHash() moveToHash()
break break
case keys[5]: case keys[5]:
openToNew() openToNew()
break break
} }
} }
const createElements = () => { const createElements = () => {
const limitElem = document.getElementsByClassName("boardBanner")[0] const limitElem = document.getElementsByClassName("boardBanner")[0]
const newNode = document.createElement("div") const newNode = document.createElement("div")
newNode.innerHTML = `<div id="drGallery" class="reply drDrag drag"> newNode.innerHTML = `<div id="drGallery" class="reply drDrag drag">
<div id="drHeader">Gallery</div> <div id="drHeader">Gallery</div>
<div id="drContainer"><img id="drImg" src="" alt="" /></div> <div id="drContainer"><img id="drImg" src="" alt="" /></div>
</div>` </div>`
const initW = "265px" const initW = "265px"
const initH = Math.round(265 * ratioMultiplier) + "px" const initH = Math.round(265 * ratioMultiplier) + "px"
const stylesheet = document.createElement("style") const stylesheet = document.createElement("style")
stylesheet.innerText = `#drGallery { stylesheet.innerText = `#drGallery {
position: absolute; position: absolute;
z-index: 9; z-index: 9;
font-weight: bold; font-weight: bold;
@ -201,43 +201,43 @@ const createElements = () => {
margin: auto; margin: auto;
}` }`
document.head.appendChild(stylesheet) document.head.appendChild(stylesheet)
document.body.insertBefore(newNode, limitElem) document.body.insertBefore(newNode, limitElem)
document.getElementsByClassName("navLinks desktop")[0].innerHTML += document.getElementsByClassName("navLinks desktop")[0].innerHTML +=
" [<a href='javascript:toggleGalleryVisibility()'>Gallery</a>]" " [<a href='javascript:toggleGalleryVisibility()'>Gallery</a>]"
} }
const collectSources = () => { const collectSources = () => {
let sources = [] let sources = []
const fileDivs = document.getElementsByClassName("fileThumb") const fileDivs = document.getElementsByClassName("fileThumb")
const hashPrefix = document const hashPrefix = document
.getElementsByClassName("postNum")[0] .getElementsByClassName("postNum")[0]
.children[0].hash.slice(0, 3) .children[0].hash.slice(0, 3)
for (let e of fileDivs) { for (let e of fileDivs) {
const s = e.href.split(".") const s = e.href.split(".")
const filetype = s[s.length - 1] const filetype = s[s.length - 1]
if (excludeWebm && filetype === "webm") { if (excludeWebm && filetype === "webm") {
continue continue
}
// div's id to post's hash (prefix x): fT12345678 => #px12345678
sources.push([e.href, hashPrefix + e.parentElement.id.slice(2)])
} }
// div's id to post's hash (prefix x): fT12345678 => #px12345678 return sources
sources.push([e.href, hashPrefix + e.parentElement.id.slice(2)])
}
return sources
} }
window.toggleGalleryVisibility = () => { window.toggleGalleryVisibility = () => {
if (baseElem.style.display === "flex") { if (baseElem.style.display === "flex") {
baseElem.style.display = "none" baseElem.style.display = "none"
document.removeEventListener("keyup", keyUpEvent, false) document.removeEventListener("keyup", keyUpEvent, false)
} else { } else {
baseElem.style.display = "flex" baseElem.style.display = "flex"
document.addEventListener("keyup", keyUpEvent, false) document.addEventListener("keyup", keyUpEvent, false)
} }
} }
const ratioMultiplier = window.innerHeight / window.innerWidth const ratioMultiplier = window.innerHeight / window.innerWidth

View File

@ -8,18 +8,18 @@
// ==/UserScript== // ==/UserScript==
window.toggleImgs = () => { window.toggleImgs = () => {
const data = document.getElementsByClassName("fileThumb") const data = document.getElementsByClassName("fileThumb")
for (let i = 0; i < data.length; ++i) { for (let i = 0; i < data.length; ++i) {
let img_data = data[i].getElementsByTagName("img") let img_data = data[i].getElementsByTagName("img")
if (img_data[0].className === "fileDeletedRes retina") { if (img_data[0].className === "fileDeletedRes retina") {
continue continue
} else if (img_data.length === 1) { } else if (img_data.length === 1) {
ImageExpansion.expand(img_data[0]) ImageExpansion.expand(img_data[0])
} else { } else {
ImageExpansion.contract(img_data[1]) ImageExpansion.contract(img_data[1])
}
} }
}
} }
const parentElem = document.getElementsByClassName("navLinks desktop")[0] const parentElem = document.getElementsByClassName("navLinks desktop")[0]

View File

@ -9,16 +9,16 @@
// ==/UserScript== // ==/UserScript==
const toggleImages = () => { const toggleImages = () => {
const mediaJpg = document.querySelectorAll("a.jpg") const mediaJpg = document.querySelectorAll("a.jpg")
const mediaPng = document.querySelectorAll("a.png") const mediaPng = document.querySelectorAll("a.png")
for (let i = 0; i < mediaJpg.length; ++i) { for (let i = 0; i < mediaJpg.length; ++i) {
mediaJpg[i].click() mediaJpg[i].click()
} }
for (let i = 0; i < mediaPng.length; ++i) { for (let i = 0; i < mediaPng.length; ++i) {
mediaPng[i].click() mediaPng[i].click()
} }
} }
const activateLink = document.createElement("button") const activateLink = document.createElement("button")