-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
73 lines (60 loc) · 1.93 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
const boards = new Map()
let running = false
let delay = 50
window.addEventListener('onBoardData', (data) => {
for (const board of data.detail) {
boards.set(board.letters, board.words)
}
})
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.message === 'start') start()
if (request.message === 'speed') delay = request.speed
})
async function start () {
const game = document.getElementsByClassName('core-letter-grid')[0]
if (running || game === undefined) return
running = true
const tiles = [...document.getElementsByClassName('letter-brick')]
const { letters, points } = getTileData(tiles)
const words = boards.get(letters.join(''))
const paths = solve(letters, words)
for (const path of paths) {
game.dispatchEvent(new MouseEvent('mousedown', getPosition(tiles[path[0]])))
for (const idx of path) {
await sleep(delay)
game.dispatchEvent(new MouseEvent('mousemove', getPosition(tiles[idx])))
}
game.dispatchEvent(new MouseEvent('mouseup', getPosition(tiles[path[path.length - 1]])))
}
running = false
}
function getTileData (tiles) {
const data = {
letters: [],
points: []
}
for (const tile of tiles) {
data.letters.push(tile.getElementsByClassName('letter')[0].innerText)
data.points.push(tile.getElementsByClassName('points')[0].innerText)
}
return data
}
function getPosition (tile) {
const rect = tile.getBoundingClientRect()
return {
clientX: (rect.left + rect.right) / 2,
clientY: (rect.top + rect.bottom) / 2
}
}
function sleep (ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
/*
* Temporary workaround until
* https://bugs.chromium.org/p/chromium/issues/detail?id=487422
* is implemented in Chrome
*/
const script = document.createElement('script')
script.setAttribute('type', 'text/javascript')
script.setAttribute('src', chrome.extension.getURL('inject.js'))
document.body.appendChild(script)