-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpopup.js
94 lines (80 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"use strict";
chrome.runtime.onMessage.addListener(function(request) {
if (request.type === "LOG_WORK_IN_JIRA_DE") {
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
func: logWork,
args: [request.payload.timeSpent, request.payload.taskDesc, request.payload.jiraPLTaskID]
});
});
}
if (request.type === "ALERT") {
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
func: (msg) => alert(msg),
args: [request.payload.msg]
});
});
}
});
function logWork(timeSpent, taskDesc, jiraPLTaskID) {
const getElements = () => {
return {
logWorkDialogTrigger: document.querySelector("#log-work"),
timeSpentInput: document.querySelector("#log-work-time-logged"),
workDescInput: document.querySelector(".jira-dialog-content textarea"),
submitBtn: document.querySelector("#log-work-submit")
};
};
const waitForLogWorkDialogOpen = () => {
const TIMEOUT = 1000;
const MAX_ATTEMPS = 60;
return new Promise((resolve, reject) => {
function findDialog(attemp = 0) {
document.title = "waiting for log dialog";
if (attemp++ === MAX_ATTEMPS) {
reject(
"Log dialog error, can't find [#log-work-time-logged] on page"
);
}
const elements = getElements();
if (elements.logWorkDialogTrigger) {
elements.logWorkDialogTrigger.click();
}
const logWorkDialog = elements.timeSpentInput;
if (logWorkDialog) {
resolve("Log dialog ready");
document.title = "log dialog ready";
} else {
setTimeout(function() {
findDialog(attemp);
}, TIMEOUT);
}
}
findDialog();
});
};
async function submit() {
try {
await waitForLogWorkDialogOpen();
const elements = getElements();
document.title = "filling log dialog";
elements.timeSpentInput.value = timeSpent;
elements.workDescInput.innerText =`${jiraPLTaskID}: ${taskDesc.replace(/"/g, "'")}`;
elements.submitBtn.click();
} catch (error) {
showAlert("Something went wrong", error);
}
}
const showAlert = msg => {
chrome.runtime.sendMessage({
type: "ALERT",
payload: {
msg
}
});
};
submit();
}