forked from fluidinfo/findings-chrome-extension
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
59 lines (51 loc) · 1.49 KB
/
background.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
// The URL to submit a clipping to Findings.
var base = 'http://findings.com/submit';
// ----------------- Utility functions for context menus -----------------
function openNewTab(clipping, info, tab){
/*
* Create a new tab with Findings looking at the given clipping value.
*/
chrome.tabs.create({
url: makeURL(clipping, info),
index: tab.index + 1
});
}
function makeURL(clipping, info){
/*
* Generate a Findings clipping submission URL given a clipping value
* and an info object containing information clipping the user event.
*/
var referer = refererFragment(info);
if (referer === ''){
return base + '?' + clippingFragment(clipping);
}
else {
return base + '?' + clippingFragment(clipping) + '&' + referer;
}
}
function clippingFragment(clipping){
/*
* Produce a clipping=xxx URL fragment for a submission request to
* Findings.
*/
return 'clipping=' + encodeURIComponent(clipping);
}
function refererFragment(info){
/*
* Produce a url=xxx refering page URL fragment for a request to
* Findings.
*/
return info.pageUrl ? 'url=' + encodeURIComponent(info.pageUrl) : '';
}
// --------------------------- Selection handling ----------------------
function getClickHandlerSelection() {
return function(info, tab){
openNewTab(info.selectionText, info, tab);
};
}
chrome.contextMenus.create({
'title' : 'Submit "%s" to findings.com',
'type' : 'normal',
'contexts' : ['selection'],
'onclick' : getClickHandlerSelection()
});