This repository has been archived by the owner on Jul 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getLists.js
106 lines (86 loc) · 2.7 KB
/
getLists.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
95
96
97
98
99
100
101
102
103
104
105
106
window.sortello = window.sortello || {interval: null};
function handleButtons (err, list) {
if (err) {
return
};
if (listSortable(list) && !listHasButton(list)) {
addButton(list);
}
if (!listSortable(list) && listHasButton(list)) {
removeButton(list);
}
if (listSortable(list) && !listHasButton(list) && !buttonIsComplete(list)) {
removeButton(list);
addButton(list);
}
}
function cardAppeared (list, cb) {
var i = 10;
if (!listSortable(list)) {
cb(false, list);
return;
}
var interval = setInterval(function () {
var oneCard = list.querySelectorAll('a.list-card')[0];
if (oneCard != null && oneCard.href.trim().length > 0) {
clearInterval(interval);
cb(false, list);
return;
}
if (i === 0) {
clearInterval(interval);
cb(true);
return;
}
i--;
}, 200);
}
function addButton (list) {
var oneCard = list.querySelectorAll('a.list-card')[0];
var oneCardHref = oneCard.href;
var oneCardUrl = oneCardHref.replace("https://trello.com/c/", "");
var oneCardId = oneCardUrl.replace(/\/(.*)/g, "")
var newElement = '<a style="height:19px;" class="list-header-extras-menu dark-hover sortello-link" title="Sort cards with Sortello" target="_blank" href="http://sortello.com/app.html?extId=' + oneCardId + '">' +
'<span class="icon-sm" style="background: url(' + chrome.runtime.getURL('icon.png') + '); background-size: contain;">' +
'</span>' +
'</a>';
var extras = list.getElementsByClassName('list-header-extras')[0];
extras.innerHTML = newElement + extras.innerHTML;
}
function removeButton (list) {
var toRemove = list.getElementsByClassName('sortello-link')[0];
toRemove.parentNode.removeChild(toRemove);
}
function listSortable (list) {
return list.getElementsByClassName('list-card').length > 2;
}
function listHasButton (list) {
return list.getElementsByClassName('sortello-link').length > 0;
}
function buttonIsComplete (list) {
var suffix = 'extId=';
var sortelloLink = list.getElementsByClassName('sortello-link');
if (sortelloLink.href === undefined) {
return false
}
return sortelloLink.href.indexOf(suffix, this.length - suffix.length) !== -1;
}
function showingTrelloBoard () {
var url = window.location.href;
var thisRegex = new RegExp('trello.com/b/');
return thisRegex.test(url);
}
function setButtons () {
var lists = document.getElementsByClassName('list');
for (var i = 0; i < lists.length; i++) {
var list = lists[i];
if(!buttonIsComplete(list)){
cardAppeared(list, handleButtons);
}
}
}
clearInterval(window.sortello.interval);
if (showingTrelloBoard()) {
setButtons();
window.sortello.interval = setInterval(setButtons, 5000);
}