-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.js
74 lines (67 loc) · 2.31 KB
/
load.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
window.onload = async () => {
let [tab] = await chrome.tabs.query({ active: true, currentWindow: true })
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: loadConfig,
}, () => {
chrome.storage.local.get(null, ({ projects, tasks }) => {
const select = document.querySelector("select#projectId")
projects.forEach(([value, name]) => {
const option = document.createElement('option')
option.value = value
option.text = name
select.appendChild(option)
})
const dl = document.createElement('dl')
dl.innerText = 'You can use these task id'
tasks.forEach(([value, name]) => {
const text = `ID: ${value} (${name})`
const dd = document.createElement('dd')
dd.innerText = text
dl.appendChild(dd)
})
const tasksDesc = document.querySelector("#task-description")
tasksDesc.classList.remove('hidden')
tasksDesc.appendChild(dl)
})
})
}
/**
* Fetch form token and store it.
*
* Extract token by requesting post form.
* TODO: Also get project ids and task ids
*/
const loadConfig = async () => {
const _getModal = async () => {
try {
const time = new Date().getTime()
const response = await fetch(`https://ssl.jobcan.jp/employee/man-hour-manage/get-man-hour-data-for-edit/unix_time/${time}`)
const { html } = await response.json()
const node = document.createElement('div')
node.innerHTML = html
return [undefined, node]
} catch (e) {
console.error(e)
return [e]
}
}
const _getOptions = (name) => {
const options = modal?.querySelectorAll(`select[name="${name}[]"] > option`)
return Array.from(options)
.map((option) => [Number.parseInt(option.value), option.text])
.filter(([value]) => !Number.isNaN(value) && value > 0)
.filter(([, name]) => name !== "(未選択)")
}
const [error, modal] = await _getModal()
console.log({ error, modal });
if (error) {
alert("You must login before use this extension.")
}
const tokenInput = modal?.querySelector('input[name="token"]')
const token = tokenInput?.value || ""
const tasks = _getOptions("tasks")
const projects = _getOptions("projects")
console.log({ token, tasks, projects });
chrome.storage.local.set({ token, tasks, projects });
}