-
Notifications
You must be signed in to change notification settings - Fork 3
/
delete-element.user.js
93 lines (78 loc) · 2.15 KB
/
delete-element.user.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
85
86
87
88
89
90
91
92
93
// ==UserScript==
// @name Delete Element
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Hold d and click to remove something to help get a clean printout or screenshot.
// @author Josh Parker
// @match *://*/*
// @icon https://www.google.com/s2/favicons?domain=wikipedia.org
// @grant none
// ==/UserScript==
(function deleteElement() {
const body = document.querySelector('body');
const lastDeleted = [];
const resetBorder = (() => {
let last = null;
return (e) => {
if (e) {
last = e;
} else if (last) {
const { target, border } = last;
target.style.removeProperty('border');
if (border) {
target.style.setProperty('border', border);
}
last = null;
}
};
})();
const addBorder = ({ target }) => {
resetBorder();
const border = target.style.getPropertyValue('border');
target.style.setProperty('border', 'dotted 2px chartreuse');
resetBorder({ target, border });
};
const handler = (event) => {
event.preventDefault();
const { target } = event;
const display = target.style.getPropertyValue('display');
target.style.setProperty('display', 'none');
lastDeleted.push({ target, display });
};
let hoverTarget = null;
let holdingD = false;
const findHoverTarget = ({ target }) => {
hoverTarget = target;
};
body.addEventListener('mouseover', findHoverTarget);
body.addEventListener('keydown', ({ code }) => {
if (code === 'KeyD') {
body.addEventListener('click', handler);
if (!holdingD) {
addBorder({ target: hoverTarget });
}
holdingD = true;
body.addEventListener('mouseover', addBorder);
}
});
body.addEventListener('keyup', ({ code }) => {
if (code === 'KeyD') {
body.removeEventListener('click', handler);
holdingD = false;
body.removeEventListener('mouseover', addBorder);
resetBorder();
}
});
body.addEventListener('keypress', ({ code }) => {
if (code === 'KeyZ') {
const e = lastDeleted.pop();
if (e) {
const { target, display } = e;
target.style.removeProperty('display');
if (display) {
target.style.setProperty('display', display);
}
}
}
});
}());