-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviewport-info.user.js
More file actions
88 lines (74 loc) · 2.09 KB
/
viewport-info.user.js
File metadata and controls
88 lines (74 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// ==UserScript==
// @name Viewport info
// @namespace https://github.com/CyrusYip/userscripts
// @match *://*/*
// @grant GM.setValue
// @grant GM.getValue
// @grant GM.registerMenuCommand
// @version 2.3.0
// @author Cyrus Yip
// @description Show innerWidth, innerHeight, and devicePixelRatio. Update them on resize event.
// ==/UserScript==
'use strict';
const showInfo = () => {
// get info
const { innerWidth, innerHeight, devicePixelRatio } = window
// const info = `innerWidth: ${innerWidth}
// innerHeight: ${innerHeight}
// devicePixelRatio: ${devicePixelRatio}`
// console.log(info)
// show info
const infoDiv = document.createElement('div')
infoDiv.classList.add('viewport-screen-info')
infoDiv.innerHTML = `${innerWidth} × ${innerHeight} (${devicePixelRatio})`
infoDiv.style.cssText = `
position: fixed;
bottom: 16px;
right: 16px;
font-size: 20px;
background-color: white;
color: green;
opacity: 75%;
z-index: 9999;
pointer-events: none;
`
document.body.appendChild(infoDiv)
}
const removeInfo = () => {
const infoDiv = document.querySelector('div.viewport-screen-info')
infoDiv?.remove()
}
const refreshInfo = () => {
removeInfo()
showInfo()
}
const detectResize = () => {
window.addEventListener('resize', refreshInfo)
}
const removeDetectResize = () => {
window.removeEventListener('resize', refreshInfo)
}
const activate = () => {
refreshInfo()
detectResize()
}
const deactivate = () => {
removeInfo()
removeDetectResize()
}
const loadConfig = async () => {
const autoShow = await GM.getValue('autoShow', false)
if (autoShow) { activate() }
}
const registerCommands = () => {
GM.registerMenuCommand('Show viewport info', activate)
GM.registerMenuCommand('Remove viewport info', deactivate)
GM.registerMenuCommand('Automatically show viewport info', () => {
GM.setValue('autoShow', true); activate()
})
GM.registerMenuCommand("Dont't automatically show viewport info", () => {
GM.setValue('autoShow', false); deactivate()
})
}
loadConfig()
registerCommands()