Advertisement
anonymous_you

2chen overlay rotator

Aug 24th, 2022
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         2chen overlay rotator
  3. // @namespace    https://2chen.moe/
  4. // @version      1.1
  5. // @description  Rotate mouse-over'd images and videos
  6. // @author       Anonymous (You)
  7. // @match        https://2chen.moe/*
  8. // @grant        none
  9. // ==/UserScript==
  10.  
  11. let rotateCW = ["ControlLeft", "AltLeft"]
  12. let rotateCCW = ["AltLeft", "ControlLeft"]
  13.  
  14. var idxCW = 0
  15. var idxCCW = 0
  16.  
  17. function getOverlayElement() {
  18.     let overlay = document.getElementById("hover-overlay")
  19.     let img = overlay.getElementsByTagName("img")[0]
  20.     let vid = overlay.getElementsByTagName("video")[0]
  21.     if (vid != null) return vid
  22.     else if(img != null) return img
  23.     else return null
  24. }
  25.  
  26. function currentRotation(transform) {
  27.     let rot = transform.match(/rotateZ\((-?\d+)deg\)/)
  28.     if (rot != null) return parseInt(rot[1])
  29.     else return 0
  30. }
  31.  
  32. function rotate(deg) {
  33.     console.log("ROT "+deg)
  34.     let elem = getOverlayElement()
  35.     if (elem != null) {
  36.         let tf = elem.style.transform
  37.         let tfNoRot = tf.replace(/ ?rotateZ\(-?\d+deg\)/, "")
  38.         let rot = (currentRotation(tf) + deg) % 360
  39.         elem.style.transform = tfNoRot + " rotateZ(" + rot + "deg)"
  40.         if (rot % 180 == 0) {
  41.             elem.style.width = "100vw"
  42.             elem.style.height = "100vh"
  43.         } else {
  44.             elem.style.width = "100vh"
  45.             elem.style.height = "100vw"
  46.         }
  47.     }
  48. }
  49.  
  50. document.addEventListener("keydown", (event) => {
  51.     if(event.code == rotateCW[idxCW]) {
  52.         idxCW++
  53.         if(idxCW == rotateCW.length) {
  54.             rotate(90)
  55.         }
  56.     }
  57.     if(event.code == rotateCCW[idxCCW]) {
  58.         idxCCW++
  59.         if(idxCCW == rotateCCW.length) {
  60.             rotate(-90)
  61.         }
  62.     }
  63. });
  64.  
  65. // keyup should reset shortcut input, but also allow repeated presses of the last key in the shortcut to trigger the rotation again
  66. document.addEventListener("keyup", (event) => {
  67.     if (event.code == rotateCW[rotateCW.length - 1]) idxCW--
  68.     else idxCW = 0
  69.     if (event.code == rotateCCW[rotateCCW.length - 1]) idxCCW--
  70.     else idxCCW = 0
  71. });
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement