-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
135 lines (112 loc) · 3.54 KB
/
script.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
function BookmarkGenerator(user) {
var Chromemarks = chrome.bookmarks,
rootFolder = 'Twitter-Marks',
bookmarks = {};
this.getBookmarkBar = function() {
return 0;
};
this.createFolder = function(folder, pID, cb) {
if (!folder) folder = rootFolder;
if (!pID) pID = getBookmarkBar();
if (!cb) cb = function(){};
Chromemarks.create({
parentId: pID,
title: folder
}, cb);
};
this.createBookmarks = function(data) {
function createUserMarks(aBTN) {
function setBookmarks(aBTN) {
data[user].forEach(function(mark, i, a){
Chromemarks.create({
parentId: aBTN.id,
title: mark.title,
url: mark.url
});
});
}
for (var user in data) {
createFolder(user, aBTN.id, setBookmarks);
}
}
Chromemarks.search(rootFolder,
function(aBTN) {
bookmarks = data;
if (aBTN.length) {
Chromemarks.removeTree(aBTN[0], function(){
createFolder(null, null, createUserMarks);
});
} else {
createFolder(null, null, createUserMarks);
}
}
);
};
this.getBookmarks = function() {
return this.bookmarks;
};
this.user = user;
}
var TwitterAPI = {
user: 'say2joe',
favorites: {/* title, url */},
API: {
domain: 'https://api.twitter.com/1.1/',
favorites: 'favorites/list.json'
},
authenticate: function() {
// placehoder for now.
},
createBookmarks: function() {
var bookmarks = new BookmarkGenerator(this.user);
bookmarks.createBookmarks(this.favorites);
},
filterFavorites: function() {
var favorites = TwitterAPI.favorites,
data = JSON.parse(this.response);
if (data.errors.length) {
if (data.errors[0].code === 215) {
TwitterAPI.authenticate();
}
}
data.forEach(function(tweet, i, a){
var user = tweet.user.name,
urls = tweet.entities.urls,
title = tweet.text.substr(0,70);
urls.forEach(function(url, i, a){
var folder = favorites[user],
bookmark = {
title: title, url: url
};
if (!group) {
favorites[folder] = [bookmark];
} else {
group.push(bookmark);
}
});
});
this.createBookmarks();
data = null;
},
getFavorites: function() {
var req = new XMLHttpRequest(),
query = '?screen_name=' + this.user,
url = this.API.domain + this.API.favorites;
req.open('GET', url + query, true);
req.onload = this.filterFavorites;
req.send();
}
};
var chromeExOAuthConfig = { // Naming convention is Google's.
'request_url': 'https://api.twitter.com/oauth/request_token',
'authorize_url': 'https://api.twitter.com/oauth/authorize',
'access_url': 'https://api.twitter.com/oauth/access_token',
'consumer_secret': 'anonymous',
'consumer_key': 'anonymous',
'app_name': 'Twitter-Marks'
};
document.addEventListener(
'DOMContentLoaded', function init() {
TwitterAPI.getFavorites();
}
);