-
Notifications
You must be signed in to change notification settings - Fork 3
/
bookmark.js
96 lines (90 loc) · 2 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
90
91
92
93
94
95
ext.bookmark = {
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);
}
},
open: function(container, inject, options)
{
options || (options = {});
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
var tab = tabs[0];
var type = '';
var do_open = function(tabdata)
{
var text = '';
if(tabdata.image && tabdata.image != '')
{
text += '![image]('+tabdata.image+') \n';
}
else if(tabdata.desc)
{
text += tabdata.desc;
}
var linkdata = {
title: type == 'image' ? '' : tab.title,
url: tab.url,
type: type,
text: text
};
ext.panel.open(container, 'BookmarkController', {
inject: inject,
linkdata: linkdata
}, {
width: 750
});
};
if(tab.url.match(/^chrome/))
{
type = 'link';
do_open({image: false});
}
else
{
ext.bookmark.get_content_type(tab.url, function(content_type) {
type = content_type.match(/^image/) ? 'image' : 'link';
if(type == 'image')
{
do_open({});
}
else
{
chrome.runtime.onMessage.addListener(function(req, sender) {
if(req.type == 'bookmark-scrape')
{
(function() { do_open(req.data); }).delay(0, this);
chrome.runtime.onMessage.removeListener(arguments.callee);
}
});
chrome.tabs.executeScript(null, {file: 'data/bookmark.scrape.js'});
}
});
}
});
}
};