-
Notifications
You must be signed in to change notification settings - Fork 6
/
bookmark.js
90 lines (85 loc) · 1.92 KB
/
bookmark.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
ext.bookmarker = {
get_content_type: function(url, finishcb)
{
if(url.match(/^https?:/))
{
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onreadystatechange = function()
{
if(xhr.readyState == 4)
{
var content_type = xhr.getResponseHeader('Content-Type');
finishcb(content_type);
}
}
xhr.send();
}
else if(url.match(/^file:/))
{
var type = 'text/html';
if(url.match(/\.(jpe?g|gif|png|bmp)$/))
{
type = 'image/'+url.replace(/^.*\.(jpe?g|gif|png|bmp)$/, '$1');
}
(function() { finishcb(type); }).delay(0, this);
}
else
{
(function() { finishcb('text/html'); }).delay(0, this);
}
},
scrape: function(options)
{
options || (options = {});
var tab = null;
var type = '';
var do_bookmark = function(data)
{
var text = '';
if(data.image && data.image != '')
{
text += '![image]('+data.image+') \n';
}
else if(data.desc)
{
text += data.desc;
}
var linkdata = {
title: type == 'image' ? '' : tab.title,
url: tab.url,
type: type,
text: text
};
if(options.complete) options.complete(linkdata);
};
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
tab = tabs[0];
if(tab.url.match(/^chrome/))
{
type = 'link';
do_bookmark({image: false});
}
else
{
ext.bookmarker.get_content_type(tab.url, function(content_type) {
type = content_type.match(/^image/) ? 'image' : 'link';
if(type == 'image')
{
do_bookmark({});
}
else
{
chrome.tabs.executeScript(tab.id, {file: 'data/bookmark.scrape.js'});
chrome.runtime.onMessage.addListener(function(request, sender) {
if(request.type != 'bookmark-scrape') return false;
chrome.runtime.onMessage.removeListener(arguments.callee);
setTimeout(function() { do_bookmark(request.data); }, 0);
return false;
});
}
});
}
});
}
};