-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
72 lines (65 loc) · 2.47 KB
/
popup.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
// state
let isAutoConnecting = false
let intervalId
const connectionRequestsCountSpan = document.getElementById('connection-count')
const autoConnectBtn = document.getElementById('auto-connect')
autoConnectBtn.onclick = async function () {
isAutoConnecting = ! isAutoConnecting
if (isAutoConnecting) {
autoConnectBtn.textContent = 'Stop Auto-Connecting'
} else {
autoConnectBtn.textContent = 'Start Auto-Connecting'
}
let [tab] = await chrome.tabs.query({ active: true, currentWindow: true })
if (! intervalId) {
intervalId = window.setInterval(function () {
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: autoConnect,
args: [isAutoConnecting]
})
const connectionRequestsCount = Number(connectionRequestsCountSpan.textContent)
connectionRequestsCountSpan.textContent = connectionRequestsCount + 1
}, 5000)
}
if (intervalId && ! isAutoConnecting) {
window.clearInterval(intervalId)
}
}
async function autoConnect(isAutoConnecting) {
// since we cannot pass args to the injected files,
// we make do with closures to simplify the injected code
const isConnectable = people => {
return people
.querySelector('.entity-result__actions button.artdeco-button .artdeco-button__text')
.textContent
.trim() == 'Connect'
}
const awaitModal = async function (ms) {
return new Promise((resolve) => {
window.setTimeout(function () {
resolve(document.querySelector('#artdeco-modal-outlet .artdeco-modal-overlay'))
}, ms)
})
}
// not the best way to query the results on the page
// but hopefully it's not too costly
const peoples = document.getElementsByClassName('entity-result__item')
for (const people of peoples) {
if (isConnectable(people)) {
// console.log('Clicking', people.querySelector('.entity-result__actions button.artdeco-button'))
people.querySelector('.entity-result__actions button.artdeco-button').click()
// console.log('Closing modal...')
/**
* we wait for some time for the modal to render on the DOM
* and then click on the Send now button
* removing the modal from the DOM was something that I could not figure out
*/
const modal = await awaitModal(500)
modal.querySelector('button[aria-label="Send now"].artdeco-button').click()
// return so that on the next run, the first of Connectable entities
// are sent a Connection request
return
}
}
}