forked from jauco/OGDs-enhancements-for-Trello
-
Notifications
You must be signed in to change notification settings - Fork 0
/
showPageAction.js
89 lines (78 loc) · 2.4 KB
/
showPageAction.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
var DEFAULT_SETTINGS = {
maxSize: 4,
ghostCards: false,
labelCards: true,
showCardNumbers: false
}
var settings = DEFAULT_SETTINGS;
var boardId = undefined;
var currentTab = undefined;
//make sure the page action is shown when a trello url is opened
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
currentTab = tabId;
trelloScrumUrlSpecificActions(tab);
});
chrome.tabs.onActivated.addListener(function (activeInfo) {
chrome.tabs.get(activeInfo.tabId, function (tab) {
currentTab = tab.id;
trelloScrumUrlSpecificActions(tab);
});
});
function trelloScrumUrlSpecificActions(tab) {
showSettingsIcon(tab);
applySettings(tab);
}
function getTrelloLocation(url) {
if (url.indexOf('https://trello.com/b/') === 0) {
return "board";
}
if (url.indexOf('https://trello.com/c/') === 0) {
return "card";
}
else {
return "other";
}
}
function getBoardId(url) {
var location = getTrelloLocation(url);
try {
if (location === "board") {
return url.match(/trello\.com\/b\/(\w+)/)[1];
}
else {
return url.match(/trello\.com\/c\/(\w+)/)[1];
}
} catch (e) {
throw new Error("can't get board id from '" + url + "'");
}
}
function applySettings(tab) {
if (getTrelloLocation(tab.url) !== "other") {
boardId = getBoardId(tab.url);
if (!localStorage[boardId]) {
settings = DEFAULT_SETTINGS;
localStorage[boardId] = JSON.stringify(settings);
} else {
settings = JSON.parse(localStorage[boardId]);
}
chrome.tabs.sendMessage(tab.id, {type: "settings", content: settings, forBoard: boardId });
}
}
function showSettingsIcon(tab) {
//don't show the icon on settings pages or non-trello pages
if (getTrelloLocation(tab.url) !== "other") {
chrome.pageAction.show(tab.id);
}
}
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
if (message.type === "getSettings") {
sendResponse(settings);
}
});
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
if (message.type === "settingUpdated") {
settings[message.name] = message.value;
localStorage[boardId] = JSON.stringify(settings);
chrome.tabs.sendMessage(currentTab, {type: "settings", content: settings });
}
});