forked from andrew-moldovan/vso-pr-auto-completion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
88 lines (77 loc) · 2.19 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
// This script runs on the popup that appears by clicking the page action button
/**
* Gets the current tab and runs the given callback if it has a VSO Pull Request open.
*/
function getCurrentTab(callback) {
// Find the active tab in the current window
var queryInfo = {
active: true,
currentWindow: true
};
// Get all the tabs that have the properties specified in queryInfo
chrome.tabs.query(queryInfo, function(tabs) {
var tab = tabs[0];
var redirectContainer = $('#redirectContainer');
var statusContainer = $('#statusContainer');
if(tab.url.indexOf('visualstudio.com') < 0 || tab.url.indexOf('/pullrequest/') < 0) {
redirectContainer.show();
statusContainer.hide();
}
else{
redirectContainer.hide();
statusContainer.show();
callback(tab);
}
});
}
/**
* Set the title and status on the page action popup
*/
function renderStatus(tabTitle, statusText) {
$('#tabTitle').html(tabTitle);
$('#status').html(statusText);
}
/**
* Stop watching the PR on current tab.
*/
function stop() {
getCurrentTab(function(tab) {
chrome.runtime.sendMessage({action: "stopTimer", tab: tab}, function (response) {});
renderStatus(tab.title, 'Nothing running');
});
}
/**
* Start watching the PR on current tab.
*/
function start() {
getCurrentTab(function(tab) {
renderStatus(tab.title, "Currently running");
chrome.runtime.sendMessage({action: "startTimer", tab: tab}, function (response) {});
});
}
/**
* Updates the status of watching the Pull Request on the current tab.
*/
function getStatus() {
getCurrentTab(function(tab) {
chrome.runtime.sendMessage({action: "getStatus", tab: tab}, function (response) {
if (response) {
renderStatus(tab.title, "Currently running");
} else {
renderStatus(tab.title, 'Nothing running');
}
});
});
}
document.addEventListener('DOMContentLoaded', function() {
// Update the status of watching PR on the current tab
getStatus();
// Attach click handler on stop button
$("#stopBtn").on("click", function(e) {
stop();
});
// Attach click handler on start button
$("#startBtn").on("click", function(e) {
start();
})
});