Version β 1 (0.1.0)
[Delicious.safariextension.git] / global.html
1 <!doctype html>
2 <html>
3         <head>
4                 <title>Delicious.safariextension</title>
5                 <script type="text/javascript" src="jquery-1.4.2.min.js"></script>
6                 <script type="text/javascript">
7                         var Delicious = window.Delicious = {
8                                 username: null,
9                                 privateKey: null,
10                                 bookmarks: [],
11                                 currentTag: null,
12                                 tags: [],
13                                 addBookmark: function(url, title) {
14                                         var deliciousURL = 'http://delicious.com/save?url=' + encodeURIComponent(url) + '&title=' + encodeURIComponent(title) + '&v=5&noui=1&jump=doclose';
15                                         if (!window.open(deliciousURL, 'deliciousuiv5', 'location=yes,links=no,scrollbars=no,toolbar=no,width=550,height=550')) {
16                                                 var tab = safari.application.activeBrowserWindow.openTab();
17                                                 tab.url = deliciousURL;
18                                         }
19                                 },
20                                 refreshBars: function() {
21                                         var bars = safari.extension.bars;
22                                         for (var i = 0; i < bars.length; i++) {
23                                                 var bar = bars[i].contentWindow;
24                                                 bar.refresh(this);
25                                         }
26                                 },
27                                 reloadBookmarksWithTag: function(tag) {
28                                         if (this.username && this.privateKey) {
29                                                 var self = this;
30                                                 var currentTag = tag;
31                                                 $.getJSON('http://feeds.delicious.com/v2/json/' + this.username + '/' + tag + '?key=' + this.privateKey + '&callback=?', function(response) {
32                                                         self.bookmarks = [];
33                                                         for (var i = 0; i < response.length; i++) {
34                                                                 var bookmark = response[i];
35                                                                 var bookmarkURL = bookmark['u'];
36                                                                 var bookmarkName = bookmark['d'];
37                                                                 self.bookmarks.push({url: bookmarkURL, name: bookmarkName});
38                                                         }
39                                                         self.currentTag = currentTag;
40                                                         self.refreshBars();
41                                                 });
42                                         } else {
43                                                 this.currentTag = null;
44                                         }
45                                 },
46                                 reloadBookmarks: function() {
47                                         this.currentTag = null;
48                                         if (this.username && this.privateKey) {
49                                                 var self = this;
50                                                 $.getJSON('http://feeds.delicious.com/v2/json/' + this.username + '?private=' + this.privateKey + '&callback=?', function(response) {
51                                                         self.bookmarks = [];
52                                                         for (var i = 0; i < response.length; i++) {
53                                                                 var bookmark = response[i];
54                                                                 var bookmarkURL = bookmark['u'];
55                                                                 var bookmarkName = bookmark['d'];
56                                                                 self.bookmarks.push({url: bookmarkURL, name: bookmarkName});
57                                                         }
58                                                         self.refreshBars();
59                                                 });
60                                         } else {
61                                                 this.bookmarks = [];
62                                         }
63                                 },
64                                 reloadTags: function() {
65                                         if (this.username && this.privateKey) {
66                                                 var self = this;
67                                                 $.getJSON('http://feeds.delicious.com/v2/json/tags/' + this.username + '?key=' + this.privateKey + '&callback=?', function(response) {
68                                                         self.tags = [];
69                                                         for (var tag in response) {
70                                                                 var count = response[tag];
71                                                                 self.tags.push({tag: tag, count: count});
72                                                         }
73                                                         self.refreshBars();
74                                                 });
75                                         } else {
76                                                 this.tags = [];
77                                         }
78                                 },
79                                 setupUser: function(privateRSSFeedURL) {
80                                         if (privateRSSFeedURL) {
81                                                 var matches = /http:\/\/feeds\.delicious\.com\/v2\/rss\/(.+)\?private=(.+)&count=15/.exec(privateRSSFeedURL);
82                                                 if (matches && matches.length == 3) {
83                                                         this.username = matches[1];
84                                                         this.privateKey = matches[2];
85                                                 } else {
86                                                         this.username = null;
87                                                         this.privateKey = null;
88                                                 }
89                                         } else {
90                                                 this.username = null;
91                                                 this.privateKey = null;
92                                         }
93                                         this.reloadBookmarks();
94                                         this.reloadTags();
95                                 },
96                                 setup: function() {
97                                         var privateRSSFeedURL = safari.extension.secureSettings.getItem('com.ithinksw.safariextensions.delicious.privateRSSFeedURL')
98                                         this.setupUser(privateRSSFeedURL);
99                                         
100                                         var self = this;
101                                         safari.extension.secureSettings.addEventListener('change', function(event) {
102                                                 if (event.key === 'com.ithinksw.safariextensions.delicious.privateRSSFeedURL') {
103                                                         self.setupUser(event.newValue);
104                                                 }
105                                         }, false);
106                                         safari.application.addEventListener("command", function(event) {
107                                                 if (event.command === "addbookmark") {
108                                                         self.addBookmark(event.target.browserWindow.activeTab.url, event.target.browserWindow.activeTab.title);
109                                                 }
110                                         }, false);
111                                         safari.application.addEventListener("validate", function(event) {
112                                                 if (event.command === "addbookmark") {
113                                                         if (event.target.browserWindow.activeTab.url) {
114                                                                 if (self.username) {
115                                                                         event.target.disabled = false;
116                                                                 } else {
117                                                                         event.target.disabled = true;
118                                                                 }
119                                                         } else {
120                                                                 event.target.disabled = true;
121                                                         }
122                                                 }
123                                         }, false);
124                                         
125                                         this.refreshBars();
126                                 },
127                         };
128                         $(function() {
129                                 Delicious.setup();
130                         });
131                 </script>
132         </head>
133         <body>
134         </body>
135 </html>