Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name 2chen overlay rotator
- // @namespace https://2chen.moe/
- // @version 1.1
- // @description Rotate mouse-over'd images and videos
- // @author Anonymous (You)
- // @match https://2chen.moe/*
- // @grant none
- // ==/UserScript==
- let rotateCW = ["ControlLeft", "AltLeft"]
- let rotateCCW = ["AltLeft", "ControlLeft"]
- var idxCW = 0
- var idxCCW = 0
- function getOverlayElement() {
- let overlay = document.getElementById("hover-overlay")
- let img = overlay.getElementsByTagName("img")[0]
- let vid = overlay.getElementsByTagName("video")[0]
- if (vid != null) return vid
- else if(img != null) return img
- else return null
- }
- function currentRotation(transform) {
- let rot = transform.match(/rotateZ\((-?\d+)deg\)/)
- if (rot != null) return parseInt(rot[1])
- else return 0
- }
- function rotate(deg) {
- console.log("ROT "+deg)
- let elem = getOverlayElement()
- if (elem != null) {
- let tf = elem.style.transform
- let tfNoRot = tf.replace(/ ?rotateZ\(-?\d+deg\)/, "")
- let rot = (currentRotation(tf) + deg) % 360
- elem.style.transform = tfNoRot + " rotateZ(" + rot + "deg)"
- if (rot % 180 == 0) {
- elem.style.width = "100vw"
- elem.style.height = "100vh"
- } else {
- elem.style.width = "100vh"
- elem.style.height = "100vw"
- }
- }
- }
- document.addEventListener("keydown", (event) => {
- if(event.code == rotateCW[idxCW]) {
- idxCW++
- if(idxCW == rotateCW.length) {
- rotate(90)
- }
- }
- if(event.code == rotateCCW[idxCCW]) {
- idxCCW++
- if(idxCCW == rotateCCW.length) {
- rotate(-90)
- }
- }
- });
- // keyup should reset shortcut input, but also allow repeated presses of the last key in the shortcut to trigger the rotation again
- document.addEventListener("keyup", (event) => {
- if (event.code == rotateCW[rotateCW.length - 1]) idxCW--
- else idxCW = 0
- if (event.code == rotateCCW[rotateCCW.length - 1]) idxCCW--
- else idxCCW = 0
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement