forked from daku/SearchMark
-
Notifications
You must be signed in to change notification settings - Fork 3
/
background.html
739 lines (613 loc) · 20.9 KB
/
background.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
<html>
<head>
<script type="text/javascript" src="libs/parseuri.js"></script>
<script language="Javascript" type="text/javascript">
// background.html: SearchMark back-end
//
// Copyright (C) 2010 Akshay Dua, and Candy Yiu
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see http://www.gnu.org/licenses/.
// =============== GLOBALS ==============
// Bookmarked page database
var SearchMarkDB = {};
// Communicate with extension UI
var gPort;
// Used to highlight searched keywords in results
var uiHighlightStart = '<span class=highlight>';
var uiHighlightEnd = '</span>';
var uiEllipses = '<b>...</b>';
var uiContextLen = -30;
// ======================== DATABASE API ==================
// Open the database
SearchMarkDB.db = null;
SearchMarkDB.open = function()
{
var dbSize = 200 * 1024 * 1024; // 200 MB
SearchMarkDB.db =
openDatabase('SearchMarkDB', '1.0', 'Bookmark Page Storage', dbSize);
}
// create the table that stores all the bookmark
// info, including the associated pages.
SearchMarkDB.createTable =
function()
{
SearchMarkDB.db.transaction(
function(tx)
{
tx.executeSql('CREATE VIRTUAL TABLE pages ' +
'USING fts3(id INTEGER PRIMARY KEY, ' +
'url TEXT, title TEXT, page TEXT, ' +
'time INTEGER, img TEXT)',
[],
getCallback("create table", "pages", 1),
getCallback("create table", "pages", 0));
tx.executeSql('CREATE TABLE IF NOT EXISTS ' +
'rawpages (id INTEGER PRIMARY KEY, htmlpage TEXT)',
[],
getCallback("create table", "rawpages", 1),
getCallback("create table", "rawpages", 0));
});
}
// add a bookmark and associated page to the database
SearchMarkDB.addBookmarkedPage =
function(newId, newUrl, newTitle, newPlainPage, newTime,
newPageImg, newHtmlPage)
{
SearchMarkDB.db.transaction(
function(tx)
{
// html-free page for searching
tx.executeSql('INSERT INTO pages(id, url, title, page, ' +
'time, img) VALUES (?,?,?,?,?,?)',
[ newId, newUrl, newTitle, newPlainPage,
newTime, newPageImg ],
getCallback("insert page", newId + " " +
newUrl, 1),
getCallback("insert page", newId + " " +
newUrl, 0));
// html page for showing cached version
tx.executeSql('INSERT INTO rawpages(id, htmlpage) ' +
'VALUES (?,?)',
[newId, newHtmlPage ],
getCallback("insert page raw", newId +
" " + newUrl, 1),
getCallback("insert page raw", newId +
" " + newUrl, 0));
});
}
// remove a bookmarked page from the database
SearchMarkDB.removeBookmarkedPage =
function(theId)
{
SearchMarkDB.db.transaction(
function(tx)
{
tx.executeSql('DELETE FROM pages WHERE id=?', [ theId ],
getCallback("remove page", theId, 1),
getCallback("remove page", theId, 0));
tx.executeSql('DELETE FROM rawpages WHERE id=?', [ theId ],
getCallback("remove page raw", theId, 1),
getCallback("remove page raw", theId, 0));
});
}
// update an already stored bookmarked page
SearchMarkDB.updateBookmarkedPage =
function(theId, theUrl, theTitle, thePlainPage, theTime,
thePageImg, theHtmlPage)
{
SearchMarkDB.db.transaction(
function(tx)
{
tx.executeSql('UPDATE pages SET url=?, ' +
'title=?, page=?, img=? WHERE id=?',
[ theUrl, theTitle, thePlainPage,
thePageImg, theId ],
getCallback("update bookmark", theUrl, 1),
getCallback("update bookmark", theUrl, 0));
tx.executeSql('UPDATE rawpages SET htmlpage=? WHERE id=?',
[ theHtmlPage, theId ],
getCallback("update bookmark", "raw " + theUrl, 1),
getCallback("update bookmark", "raw " + theUrl, 0));
});
}
// get all bookmark URLs. Callback function can
// be provided use the results as necessary.
SearchMarkDB.getStoredBookmarks =
function()
{
SearchMarkDB.db.transaction(
function(tx)
{
tx.executeSql('SELECT id,url,title FROM pages',
[],
getCallback("show db", "pages", 1),
getCallback("show db", "pages", 0));
tx.executeSql('SELECT id FROM rawpages',
[],
getCallback("show db", "raw", 1),
getCallback("show db", "raw", 0));
});
}
// Supports the cached page feature. Returns cached raw html page.
SearchMarkDB.getRawHtmlPage =
function (id, callback)
{
SearchMarkDB.db.transaction(
function(tx)
{
tx.executeSql('SELECT htmlpage FROM rawpages ' +
'WHERE id = ?',
[id],
callback,
getCallback("get page", "raw", 0));
});
}
SearchMarkDB.doSearch =
function(callback, keywords)
{
SearchMarkDB.db.transaction(
function(tx)
{
tx.executeSql('SELECT id,url,title, img, ' +
'snippet(pages, "' + uiHighlightStart +
'", "' + uiHighlightEnd +
'", "' + uiEllipses +
'", -1, ' + uiContextLen + ') ' +
'as snippet FROM pages WHERE ' +
'pages MATCH ' + keywords + ' ' +
'ORDER BY time DESC',
[],
callback,
getCallback("search pages", "malformed input", 0));
});
}
// clear all stored information.
SearchMarkDB.clear =
function()
{
SearchMarkDB.db.transaction(
function(tx)
{
tx.executeSql('DELETE FROM pages', [],
getCallback("clear table", "pages", 1),
getCallback("clear table", "pages", 0));
tx.executeSql('DELETE FROM rawpages', [],
getCallback("clear table", "rawpages", 1),
getCallback("clear table", "rawpages", 0));
});
}
// remove the table and all stored information
SearchMarkDB.purge =
function()
{
SearchMarkDB.db.transaction(
function(tx)
{
tx.executeSql('DROP TABLE pages', [],
getCallback("delete table", "pages", 1),
getCallback("delete table", "pages", 0));
tx.executeSql('DROP TABLE rawpages', [],
getCallback("delete table", "rawpages", 1),
getCallback("delete table", "rawpages", 0));
});
}
// ========================== CORE ===============
// prepare to initialize
// open the database each time extension loads.
SearchMarkDB.open();
console.debug("Opened SearchMark database.");
localStorage['newversion'] = 2.5;
// Important for new installs
if(!localStorage['oldversion'])
{ // not defined
// set to a version before upgrade functionality ever existed
localStorage['oldversion'] = 1.1;
}
if(localStorage['newversion'] > localStorage['oldversion'])
{
// will not be true for new installs
if(localStorage['initialized'])
{ // already installed. Do upgrade.
console.log("Upgrading to version " +
localStorage['newversion']);
doUpgrade();
}
localStorage['oldversion'] = localStorage['newversion'];
}
init();
chrome.browserAction.onClicked.addListener(
function(tab)
{
chrome.tabs.create(
{'url' : 'SearchMarkUI.html'},
function(newTab) {});
});
chrome.extension.onRequest.addListener(handleRequest);
chrome.bookmarks.onChanged.addListener(
function(id, changeInfo)
{
if (!localStorage['initialized'])
return;
getAndStoreBookmarkContent(
{id : id,
url : changeInfo.url,
title : changeInfo.title,
time : 0},
SearchMarkDB.updateBookmarkedPage);
});
chrome.bookmarks.onCreated.addListener(
function(id, newBookmark)
{
localStorage['totalbookmarks']++;
if (!localStorage['initialized'])
return;
getAndStoreBookmarkContent(
{id : id,
url : newBookmark.url,
title : newBookmark.title,
time : newBookmark.dateAdded},
SearchMarkDB.addBookmarkedPage);
});
chrome.bookmarks.onRemoved.addListener(
function(id, removeInfo)
{
localStorage['totalbookmarks']--;
if (!localStorage['initialized'])
return;
SearchMarkDB.removeBookmarkedPage(id);
});
// experimental APIs require user to start chrome with a specific option
// flag from the command line. So, not using for now.
// chrome.experimental.omnibox.onInputEntered.addListener(
// function(keywords) {
// handleRequest({method: 'search', keywords: keywords},
// background, function() {});
// }
// );
// ================= CORE API ===================
function init()
{
console.log("Initializing...");
// if bookmarks in DB not in sync with actual bookmarks
if(localStorage['added'] && localStorage['totalbookmarks'] &&
localStorage['added'] != localStorage['totalbookmarks'])
cleanupStorage();
// initialize once only. Populate the database
// by retrieving and storing bookmarked pages, and
// URLs.
if (!localStorage['initialized'] ||
localStorage['initialized'] == 0)
{
SearchMarkDB.createTable();
chrome.bookmarks.getTree(
function(bookmarks)
{
localStorage['added'] = 0;
localStorage['totalbookmarks'] = 0;
initBookmarkDatabase(bookmarks);
});
// number of times the welcome page was opened
localStorage['uivisits'] = 0;
localStorage['initialized'] = 1;
} else {
localStorage['initialized']++;
}
// debug and test
// getUrlContent("http://www.sqlite.org/lang_altertable.html");
}
// any upgrade functionality should be placed here
function doUpgrade()
{
if(localStorage['oldversion'])
cleanupStorage();
}
// clean up stored configuration variables
function cleanupStorage()
{
console.log("Cleaning up...");
console.log("Clearing database tables");
SearchMarkDB.clear();
console.log("Removing the tables");
SearchMarkDB.purge();
console.log("Setting to 'not initialized'");
localStorage['initialized'] = 0;
}
function handleRequest(request, sender, callback)
{
if (request.method == 'search') {
gPort = chrome.extension.connect( {name : "uiToBackend"});
console.debug("search " + request.keywords);
SearchMarkDB.doSearch(searchBookmarkedPagesCb,
"'" + request.keywords + "'");
callback();
} else if (request.method == 'cached') {
SearchMarkDB.getRawHtmlPage(request.bookmarkid, displayRawPage);
console.debug("cache request " + request.bookmarkid);
callback();
} else {
callback();
}
}
function displayRawPage(tx, r)
{
if(r.rows.length) {
chrome.tabs.create(
{url: 'rawPageView.html', selected: true},
function (tab)
{
// connect to tab that will show the raw page
var port = chrome.extension.connect({name:
"rawPageView"});
// send the raw page
port.postMessage(r.rows.item(0).htmlpage);
// done.
port.disconnect();
});
} else {
console.log("Unexpected error: this page should have " +
"been cached. Please file a bug report " +
"at <todo:put github url here>");
}
}
function searchBookmarkedPagesCb(tx, r)
{
var result = {};
for ( var i = 0; i < r.rows.length; i++) {
// deprecated, remove eventually
result.matchType = "page";
result.id = r.rows.item(i).id;
result.url = r.rows.item(i).url;
result.title = r.rows.item(i).title;
result.text = r.rows.item(i).snippet;
result.img = r.rows.item(i).img;
console.log(result.img);
gPort.postMessage(result);
result = {};
}
result.matchType = "DONE";
gPort.postMessage(result);
}
function removeHTMLfromPage(page)
{
// reduce spaces, remove new lines
var pagetxt = page.replace(/\s+/gm, " ");
// remove 'script', 'head', 'style' tags
pagetxt = pagetxt.replace(/<\s*?head.*?>.*?<\s*?\/\s*?head\s*?>/i, " ");
pagetxt = pagetxt.replace(/<\s*?script.*?>.*?<\s*?\/\s*?script\s*?>/gi, " ");
pagetxt = pagetxt.replace(/<\s*?style.*?>.*?<\s*?\/\s*?style\s*?>/gi, " ");
// Now remove other tags
pagetxt = pagetxt.replace(/<.*?\/?>/g, " ");
// Remove symbols
pagetxt = pagetxt.replace(/&.*?;/g, " ");
// Remove comment markers
pagetxt = pagetxt.replace(/(<!--|-->)/g, " ");
// After all the filtering, need to fix up spaces again
pagetxt = pagetxt.replace(/\s+/gm, " ");
return pagetxt;
}
function extractPageImg(page, url)
{
// // look for first top-level heading
// var idx = page.search(/<h[1-3]/i);
// // if idx = -1 (not found)
// // get first image
// if(idx == -1)
// idx = 0;
var imgstart = page.indexOf("<img", 0);
// if no image, screw it
if(imgstart == -1)
return "";
// var imgend = page.indexOf(">", imgstart);
// // if malformed html, screw it
// if(imgend == -1)
// return "";
// var imgtag = page.substring(imgstart, imgend + 1);
// fix src url of image if necessary
var srcstart = page.indexOf("src", imgstart);
// malformed img tag
if(srcstart == -1)
return "";
var quote = '"';
srcstart = page.indexOf(quote, srcstart);
// maybe its a single quote
if(srcstart == -1)
{
quote = "'";
srcstart = page.indexOf(quote, srcstart);
}
// malformed img tag
if(srcstart == -1)
return "";
var srcend = page.indexOf(quote, srcstart + 1);
// malformed img tag
if(srcend == -1)
return "";
var src = page.substring(srcstart + 1, srcend);
if(src.indexOf("://", 0) != -1)
{ // full path
;
}
else if(src[0] == '/')
{ // path from host url
if(src[1] == '/')
src = src.substring(2);
else
src = parseUri(url).host + src;
}
else
{ // relative path
src = parseUri(url).host + parseUri(url).directory + src;
}
console.debug("extracted image: " + src + ", url: " + url);
return src;
}
// debug and test function. Not called from core
function getUrlContent(url)
{
try {
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onreadystatechange = function()
{
try {
if (this.readyState == 4) {
console.log("got page. extracting img url");
imgurl = extractPageImg(this.responseText, url);
this.abort();
}
} catch (e) {
console.error(e.message);
}
}
xhr.send();
} catch (e) {
console.error(e.message + bookmark.url);
}
}
function getAndStoreBookmarkContent(bookmark, storeInDB)
{
try {
var xhr = new XMLHttpRequest();
xhr.open("GET", bookmark.url, true);
xhr.onreadystatechange = function()
{
try {
if (this.readyState == 4) {
var pageNoHtml = removeHTMLfromPage(this.responseText);
// add page to database
storeInDB(bookmark.id, bookmark.url,
bookmark.title, pageNoHtml,
bookmark.dateAdded,
extractPageImg(this.responseText,
bookmark.url),
this.responseText);
this.abort();
}
} catch (e) {
console.log(e.message);
storeInDB(bookmark.id, bookmark.url, bookmark.title,
bookmark.dateAdded, "", "", "");
}
}
xhr.send();
} catch (e) {
console.log(e.message + bookmark.url);
storeInDB(bookmark.id, bookmark.url, bookmark.title,
bookmark.dateAdded, "", "", "");
}
}
function initBookmarkDatabase(bookmarks)
{
bookmarks.forEach(
function(bookmark)
{
if (bookmark.url &&
bookmark.url.match("^https?://*"))
{ // url exists and is well formed
console.debug("Adding " + bookmark.url);
localStorage['totalbookmarks']++;
getAndStoreBookmarkContent(bookmark,
SearchMarkDB.addBookmarkedPage);
} else {
console.debug("Skipping. " + bookmark.url);
}
if (bookmark.children)
initBookmarkDatabase(bookmark.children);
});
}
function getCallback(cbname, msg, type)
{
switch (cbname) {
case "show db":
if (type == 1)
return function(tx, r)
{
for ( var i = 0; i < r.rows.length; i++) {
console.log("Stored. " + msg + " " +
r.rows.item(i).url);
}
}
else
return function(tx, r)
{
console.debug("failed: " + cbname + " " + msg);
console.log(" " + e.message);
}
break;
case "search pages":
if (type == 1) // success callback
return function(tx, r)
{
console.debug("succeded: " + cbname + " " + msg);
}
else
return function(tx, e)
{
console.debug("failed: " + cbname + " " + msg);
console.log(" " + e.message);
// search pages failed, tell user
var result = {};
result.matchType = "DONE";
result.error = 'Sorry, I am not sure what you are ' +
'looking for. Could you be missing a quote (") ' +
'while searching for a phrase?';
gPort.postMessage(result);
}
break;
case "insert page raw":
if (type == 1) // success callback
return function(tx, r)
{
console.debug("succeded: " + cbname + " " + msg);
localStorage['added']++;
}
else
// failure callback
return function(tx, e)
{
console.debug("failed: " + cbname + " " + msg);
console.log(" " + e.message);
}
break;
case "remove page raw":
if (type == 1) // success callback
return function(tx, r)
{
console.debug("succeded: " + cbname + " " + msg);
localStorage['added']--;
}
else
// failure callback
return function(tx, e)
{
console.debug("failed: " + cbname + " " + msg);
console.log(" " + e.message);
}
break;
default:
if (type == 1) // success callback
return function(tx, r)
{
console.debug("succeded: " + cbname + " " + msg);
}
else
// failure callback
return function(tx, e)
{
console.debug("failed: " + cbname + " " + msg);
console.log(" " + e.message);
}
}
}
</script>
</head>
</html>