-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyscript.js
75 lines (64 loc) · 2.16 KB
/
myscript.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
// Insert str2 into str1 at position
function insertStr(str1, str2, position) {
return str1.substr(0, position) + str2 + str1.substr(position);
}
// Returns text with spans surrounding
// Text that matches query string
function highlightTextWithQuery(string, strQuery) {
var regexp = RegExp(strQuery.replace(/\s+/g, '.*'), 'i'); // Case insensitive
var match = string.match(regexp);
var spanOpen = "<span class='bm-highlight'>";
var spanClose = "</span>";
var html = "";
if (match) {
html = insertStr(string, spanOpen, match.index);
html = insertStr(html, spanClose, match.index + match[0].length + spanOpen.length);
return html;
}
return string;
}
// Builds HTML to render for one bookmark
function buildBookmarkElement(title, url, query) {
var html = '';
html += "<div class='bm-elem'>" +
"<a href='" + url + "'>" +
"<span class='bm-title'>" + highlightTextWithQuery(title, query) + "</span><br>" +
"<span class='bm-url'>" + highlightTextWithQuery(url, query).substring(0, 99) +
"</span></a>" +
"</div>";
return html;
}
// Ask background script to query on bookmarks
function queryBookmarks() {
var query = $("#lst-ib").val();
chrome.runtime.sendMessage({bookmarks: query}, function(bookmarks) {
var htmlToInsert = "<div id='bookmarks'>";
bookmarks.forEach(function(bookmark) {
// Title equals url if title is empty
var title = bookmark['title'].length ? bookmark['title'] : bookmark['url'];
var url = bookmark['url'];
htmlToInsert += buildBookmarkElement(title, url, query);
});
htmlToInsert += "</div>";
if ($('#bookmarks').length)
$('#bookmarks').remove();
if (bookmarks.length)
$("#res").before(htmlToInsert);
});
}
// Waiting for page to be completely loaded
// Because we need to be sure
// That the search bar input is accessible
$(document).ready(function() {
// Break execution if we're not on google.*
if (!window.location.hostname.match(/www\.google\..*/))
return -1;
queryBookmarks(); // First call when page loads
$("#lst-ib").on(
"keyup input",
$.debounce(
300,
queryBookmarks
)
);
});