forked from Athou/commafeed-safari
-
Notifications
You must be signed in to change notification settings - Fork 0
/
global.html
71 lines (62 loc) · 1.64 KB
/
global.html
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
<!DOCTYPE HTML>
<script src="jquery-1.9.1.min.js"></script>
<script>
safari.application.addEventListener("command", performCommand, false);
safari.extension.settings.addEventListener("change", settingChanged, false);
var timer = null;
function settingChanged(event){
if (event.key == "updateTime") {
clearTimeout(timer);
getUnreadCount();
}
}
function hasCommafeedOpen(){
var windows = safari.application.browserWindows;
for (var i=0;i<windows.length;i++){
var tabs = windows[i].tabs;
for (var j=0;j<tabs.length;j++){
if (typeof tabs[j].url != 'undefined'){
if(tabs[j].url.indexOf("commafeed.com")!=-1){
windows[i].activate();
tabs[j].activate();
return true;
}
}
}
}
return false;
}
function performCommand(event) {
if (event.command == "open-commafeed") {
if(!hasCommafeedOpen()){
if(safari.application.activeBrowserWindow.activeTab.url){
var newTab = safari.application.activeBrowserWindow.openTab();
newTab.url = "https://commafeed.com/";
}
else{
safari.application.activeBrowserWindow.activeTab.url="https://commafeed.com/";
}
}
}
}
function updateUnreadCount(data){
var sum = 0;
for(var i=0;i<data.length;i++){
sum+=data[i].unreadCount;
}
var toolbarItems = safari.extension.toolbarItems;
for (var i=0;i<toolbarItems.length;i++){
if(toolbarItems[i] && toolbarItems[i].identifier=="commafeed")
toolbarItems[i].badge=sum;
}
}
function getUnreadCount(){
$.ajax({
url: "https://www.commafeed.com/rest/category/unreadCount",
dataType: "json",
success: updateUnreadCount
});
timer = setTimeout(getUnreadCount,safari.extension.settings.updateTime);
}
getUnreadCount();
</script>