-
Notifications
You must be signed in to change notification settings - Fork 0
/
inject.js
82 lines (64 loc) · 2.19 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
if (!document.injected) {
let hex = ''
let colors = []
chrome.runtime.onMessage.addListener(
function (request) {
if (request.type === 'setPuzzle') {
console.log('Setting Puzzle');
hex = request.hex
colors = request.colors
if (hex !== '') constructBomb(hex, colors)
}
});
document.injected = true
}
function constructBomb(hex, colors) {
if (document.getElementById('bomb-container') === null) {
console.log('Constructing');
const bomb = document.createElement('div')
bomb.setAttribute('id', 'bomb-container')
const interface = document.createElement('div')
interface.setAttribute('id', 'bomb-interface')
const display = document.createElement('p')
display.setAttribute('id', 'bomb-display')
display.innerText = hex
const input = document.createElement('div')
input.setAttribute('id', 'bomb-input')
colors.forEach(function (color) {
const square = document.createElement('button')
square.setAttribute('class', 'bomb-button')
square.setAttribute('style', `background-color: ${color}`)
square.addEventListener('click', function () { submitGuess(color) })
input.appendChild(square)
})
interface.appendChild(display)
interface.appendChild(input)
bomb.appendChild(interface)
const [body] = document.getElementsByTagName('body')
body.appendChild(bomb)
}
}
function bombTriggered() {
const display = document.getElementById('bomb-display')
display.innerText = 'Detonating Bomb'
setTimeout(cleanupBomb, 1000)
}
function bombDefused() {
const display = document.getElementById('bomb-display')
display.innerText = 'Bomb Defused'
display.style.color = '#2288ff'
setTimeout(cleanupBomb, 1000)
}
function cleanupBomb() {
const bombDebris = document.getElementById('bomb-container')
const [body] = document.getElementsByTagName('body')
body.removeChild(bombDebris)
}
function submitGuess(color) {
const port = chrome.runtime.connect({ name: "defusalPort" });
port.postMessage({ type: "guess", color: color });
port.onMessage.addListener(function (response) {
if (response.defused) bombDefused()
else if (!response.defused) bombTriggered()
});
}