-
Notifications
You must be signed in to change notification settings - Fork 1
/
content.js
90 lines (78 loc) · 3.48 KB
/
content.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
let clickedElement = null; // the clicked element
let clickedElementCopy = null; // copy of the clicked element for posterity
let originalOutlineStyle = null; // contains the original styles of the element
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
if (request.deleteElement) {
$(clickedElement).remove();
}
sendResponse({deleted: true})
});
const fadeOutFunc = function (clickedElement, highlightColor, originalColor, timeInt, outlineWidth, outlineStyle, highlightOpacity, fadeOutlineAfter) {
highlightColor = /((\s*?\d{1,3}\s*?,?\s*?){3})/.exec(highlightColor);
highlightColor = (originalColor.length > 1) ? highlightColor[0] : highlightColor;
setTimeout(function () {
$({alpha: 1}).animate({alpha: 0}, {
duration: parseInt(timeInt) * 1000,
step: function () {
$(clickedElement).css('outline', `${outlineWidth} ${outlineStyle} rgba(${highlightColor},${this.alpha})`);
}
});
}, parseInt(fadeOutlineAfter));
};
const exit = function (event, config) {
if (contains.call(config['event']['button']['exit'], event.button) && originalOutlineStyle !== null) {
$(clickedElementCopy).css('outline', originalOutlineStyle);
log(`ContextDeleter restored styles to: ${clickedElementCopy}`);
return true;
}
return false;
};
$(document).on('mousedown', 'body', function (event) {
chrome.storage.sync.get({
highlightColor: '0,1,255',
highlightOpacity: 0.3,
outlineStyle: 'solid',
outlineWidth: '1px',
debugEnabled: false,
fadeOutline: true,
fadeOutlineTime: '1',
fadeOutlineAfter: '25',
}, function (items) {
const {
highlightColor,
highlightOpacity,
outlineStyle,
outlineWidth,
debugEnabled,
fadeOutline,
fadeOutlineTime,
fadeOutlineAfter
} = items;
const outline = (highlightColor === "transparent" || outlineStyle === 'hidden') ? 'none' : `${outlineWidth} ${outlineStyle} rgba(${highlightColor}, ${highlightOpacity})`;
const config = {
'event': {
'button': {
'target': [2], // buttons to listen to target an element
'exit': [1, 0] // buttons to listen to exit
}
}
};
if (contains.call(config['event']['button']['target'], event.button)) {
// in case of re-targeting, reset the outline style of the old targeted element
if (clickedElementCopy !== null && $(clickedElementCopy).css('outline') !== originalOutlineStyle) {
$(clickedElementCopy).css('outline', originalOutlineStyle);
}
clickedElement = event.target;
originalOutlineStyle = $(clickedElement).css('outline'); // store the original outline style
// set the target outline
$(clickedElement).css('outline', config['outline']);
if (fadeOutline === true) {
fadeOutFunc(clickedElement, highlightColor, originalOutlineStyle, fadeOutlineTime, outlineWidth, outlineStyle, highlightOpacity, fadeOutlineAfter);
}
// make a copy of the clicked element
clickedElementCopy = (clickedElement !== clickedElementCopy) ? clickedElement : clickedElementCopy;
log(`ContextDeleterTarget: ${clickedElement}`)
}
exit(event, config);
});
});