-
Notifications
You must be signed in to change notification settings - Fork 5
/
context.js
115 lines (99 loc) · 3.38 KB
/
context.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// --------------------------- Page --------------------------
chrome.contextMenus.create({
'title' : 'This page in the sidebar',
'type' : 'normal',
'contexts' : ['page'],
'onclick' : function(info, tab){
openInSidebar(info.pageUrl, info, tab);
}
});
chrome.contextMenus.create({
'title' : 'This page on loveme.do',
'type' : 'normal',
'contexts' : ['page'],
'onclick' : function(info, tab){
openNewTab(info.pageUrl, info, tab);
}
});
// --------------------------- Image --------------------------
chrome.contextMenus.create({
'title' : 'This image in the sidebar',
'type' : 'normal',
'contexts' : ['image'],
'onclick' : function(info, tab){
openInSidebar(info.srcUrl, info, tab);
}
});
chrome.contextMenus.create({
'title' : 'This image on loveme.do',
'type' : 'normal',
'contexts' : ['image'],
'onclick' : function(info, tab){
openNewTab(info.srcUrl, info, tab);
}
});
// --------------------------- Openers --------------------------
function openInSidebar(about, info, tab){
/*
* Open the sidebar, looking at the given about value.
*/
var port = chrome.tabs.connect(tab.id, {name: 'sidebar'});
port.postMessage({
about: about,
action: 'show sidebar'
});
}
function openNewTab(about, info, tab){
/*
* Create a new tab with the object browser looking at the given about value.
*/
chrome.tabs.create({
index: tab.index + 1,
url: 'http://' + lovemedoHost + '/about/' + encodeURIComponent(about)
});
}
// ---------------------- Dynamic context menu items ------------
// contextMenuItems has attributes that are the text of current
// context menu items. Its values are objects with three attributes,
// 'context' (either 'link' or 'selection'), 'gotoMenuItem' and
// 'sidebarMenuItem', the menu item indices returned by
// chrome.contextMenus.create.
var contextMenuItems = {};
var addContextMenuItem = function(text, context){
// Add (possibly truncated) 'text' to the context menu, if not already present.
text = (text.length < 50 ? text : text.slice(0, 47) + '...').replace(/\n+/g, ' ');
if (typeof contextMenuItems[text] === 'undefined'){
var sidebarMenuItem = chrome.contextMenus.create({
'title' : text + ' in the sidebar',
'type' : 'normal',
'contexts' : [context],
'onclick' : function(info, tab){
openInSidebar(text, info, tab);
}
});
var gotoMenuItem = chrome.contextMenus.create({
'title' : text + ' on loveme.do',
'type' : 'normal',
'contexts' : [context],
'onclick' : function(info, tab){
openNewTab(text, info, tab);
}
});
contextMenuItems[text] = {
context: context,
gotoMenuItem: gotoMenuItem,
sidebarMenuItem: sidebarMenuItem
};
}
};
var removeContextMenuItemsByContext = function(context){
var text;
for (text in contextMenuItems){
if (typeof contextMenuItems[text] !== 'undefined' &&
contextMenuItems[text].context === context){
chrome.contextMenus.remove(contextMenuItems[text].gotoMenuItem);
chrome.contextMenus.remove(contextMenuItems[text].sidebarMenuItem);
delete contextMenuItems[text];
}
}
};