-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresearchAssist.js
125 lines (116 loc) · 4.15 KB
/
researchAssist.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
var rAssist;
var isSearching = false;
var search = "";
var prevSearch = "";
var searchFolder;
var urlsCheck = "chrome:\/\/*.|.*www.google.com\/.*q=.*|.*www.bing.com\/*q=*|.*www.yahoo.com\/.*t=.*|.*www.ask.com\/.*q=.*|.*search.aol.com\/.*q=.*|.*www.searchaol.com\/.*q=.*|.*.wow.com\/.*q=.*|.*www.webcrawler.com\/.*q=.*|.*.infospace.com\/.*q=.*|.*www.dogpile.com\/.*q=.*";
setBookmarkRoot();
/**
* Creates a bookmark folder for all of research Assist's searches to go into.
*/
function setBookmarkRoot() {
chrome.bookmarks.search({'title': 'Research Assist'}, function (tmp) {
if (tmp.length < 1) {
chrome.bookmarks.create({'title': 'Research Assist'},
function(newFolder) {
rAssist = newFolder;
});
} else {
rAssist = tmp[0]; //node is hopefully in the first slot here.
}
});
}
/**
* Takes a searchTerm and creates a bookmark if it doesn't already exist.
* After done, calls the callback supplying it with the bookmark folder.
*/
function getMarkSearchRoot(searchTerm, callback) {
console.assert(typeof searchTerm == "string", "should be a string");
chrome.bookmarks.search({'title': searchTerm },
function (bmItems) {
if (bmItems.length > 0)
callback(bmItems[0]);
else {
chrome.bookmarks.create({"title": searchTerm, 'parentId': rAssist.id }, function (bmRoot) { // create folder
callback(bmRoot);
});
}
});
}
function checkURL(url) {
if (url.length == 0)
return false;
else
return (!url.match(urlsCheck));
}
function createMark(searchTerm, url, title, callback) {
if (!checkURL(url)) //make sure url is "valid" i.e. not a search
return;
getMarkSearchRoot(searchTerm, function (sFolder) {
searchFolder = sFolder;
chrome.bookmarks.search({'title':title, 'url':url }, function (bmsFound) {
var bookmark=bmsFound[0];
if (bmsFound.length < 1)
{
chrome.bookmarks.create({'parentId': searchFolder.id, 'title':title, 'url':url }, function(bm) {
bookmark = bm;
});
}
callback(bookmark);
});
});
}
/**
* Get the current Title.
*
* @param {function(tab)} callback - called when the current tab
* is found.
*/
function getCurrentTab(callback) {
var queryInfo = {
active: true,
currentWindow: true
};
chrome.tabs.query(queryInfo, function(tabs) {
var tab = tabs[0];
console.assert(typeof tab.title== "string", "Tab's Title should be a string");
callback(tab);
});
}
function getTabByURL(urlV,callback) {
var queryInfo = {
currentWindow:true,
url:urlV
};
chrome.tabs.query(queryInfo, function(tabs) {
var tab = tabs[0];
console.assert(typeof tab.title== "string", "Tab's Title should be a string");
callback(tab);
});
}
chrome.webRequest.onCompleted.addListener(function (e) {
var queryUrl = e.url.split("q=")[1] || e.url.split("t=")[1];
var query = queryUrl.split("&")[0].split("%20").join(" ").split("+").join(" ").split("%27").join("'"); //replace "%20" or "+" with " "
if (query.length > 0)
{
search = query;
isSearching = true;
chrome.alarms.create("RAsearchAlarm", {'delayInMinutes':7.0})
}
}, {urls: ["*://www.google.com/*q=*", "*://www.bing.com/*q=*", "*://www.yahoo.com/*t=*", "*://www.ask.com/*q=*", "*://search.aol.com/*q=*", "*://www.searchaol.com/*q=*", "*://*.wow.com/*q=*",
"*://www.webcrawler.com/*q=*", "*://*.infospace.com/*q=*", "*://www.dogpile.com/*q=*"]});
chrome.webRequest.onCompleted.addListener(function (e) {
if (isSearching) {
getCurrentTab(function(tab) {
prevSearch = search;
createMark(search, tab.url, tab.title, function(bm) {
return bm;
});
});
}
}, {urls: ["*://*/*"], types: ["main_frame"]});
chrome.alarms.onAlarm.addListener(function (alarm){
if (alarm.name == "RAsearchAlarm") {
isSearching = false;
}
});