-
Notifications
You must be signed in to change notification settings - Fork 4
/
inject.js
84 lines (73 loc) · 2.46 KB
/
inject.js
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
;(() => {
const scriptUrl = new URL(document.currentScript.src)
const rootUrl = scriptUrl.origin + scriptUrl.pathname
const cssText = `
:root {
--petpet-icon-size-def: 3.6em;
--petpet-body-width-def: 28em;
}
.petpet-iframe {
position: fixed;
width: min(100%, var(--petpet-body-width, var(--petpet-body-width-def)));
height: 100vh;
top: 0;
right: 0;
border: 0;
border-left: 1px dashed;
transition: right 1s;
z-index: 9998;
}
.petpet-icon {
position: fixed;
right: var(--petpet-icon-right, 0);
top: var(--petpet-icon-top, 0);
cursor: pointer;
background: url(${new URL('./icons/icon-128x128.png', rootUrl)}) center/100%;
width: var(--petpet-icon-size, var(--petpet-icon-size-def));
height: var(--petpet-icon-size, var(--petpet-icon-size-def));
z-index: 9999;
}
.petpet-iframe.petpet-hide {
right: calc(var(--petpet-body-width, var(--petpet-body-width-def)) * -1 - 4px);
}
`
const style = document.createElement('style')
style.innerText = cssText
document.head.appendChild(style)
const iframe = document.createElement('iframe')
iframe.classList.add('petpet-iframe', 'petpet-hide');
iframe.src = new URL('./index.html', rootUrl).toString()
const icon = document.createElement('div')
icon.classList.add('petpet-icon')
document.body.appendChild(icon)
document.body.appendChild(iframe)
let isDragging = false
const offset = {x: 0, y: 0}
const mPos = {x: 0, y: 0}
const startDragging = e => {
isDragging = true
e.preventDefault()
const {left, top, width, height} = icon.getBoundingClientRect()
offset.x = e.clientX - (left + width / 2);
offset.y = e.clientY - (top + height / 2);
mPos.x = e.clientX
mPos.y = e.clientY
}
const stopDragging = e => {
isDragging = false
icon.style.cursor = 'pointer'
if (Math.abs(mPos.x - e.clientX) < 10 && Math.abs(mPos.y - e.clientY) < 10) {
iframe.classList.toggle('petpet-hide')
}
}
const drag = (e) => {
if (isDragging) {
icon.style.cursor = 'grabbing'
icon.style.left = `${e.clientX - offset.x - icon.offsetWidth / 2}px`;
icon.style.top = `${e.clientY - offset.y - icon.offsetHeight / 2}px`;
}
}
icon.addEventListener('mousedown', startDragging)
icon.addEventListener('mouseup', stopDragging)
document.body.addEventListener('mousemove', drag)
})();