forked from Kenshin/simpread
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu.js
More file actions
105 lines (92 loc) · 2.72 KB
/
menu.js
File metadata and controls
105 lines (92 loc) · 2.72 KB
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
console.log( "=== simpread menu load ===" )
import {storage} from 'storage';
import {browser} from 'browser';
import * as msg from 'message';
/**
* Create context menus
*/
const context = {
focus : { id: "", menu: {} },
read : { id: "", menu: {} },
link : { id: "", menu: {} },
list : { id: "", menu: {} },
},
menu = {
"type" : "normal",
"contexts" : [ "all" ],
"documentUrlPatterns" : [ "http://*/*" , "https://*/*" ]
};
Object.assign( context.focus.menu, menu, { id: "focus", "title" : "聚焦模式" });
Object.assign( context.read.menu, menu, { id: "read", "title" : "阅读模式" });
Object.assign( context.list.menu, menu, { id: "list", "title" : "打开稍后读" });
Object.assign( context.link.menu, menu, { id: "link", "title" : "使用阅读模式打开此链接", contexts: [ "link" ] });
/**
* Listen contextMenus message
*/
function onClicked( callback ) {
browser.contextMenus.onClicked.addListener( callback );
}
/**
* Create all context menu
*/
function createAll() {
storage.option.menu.focus &&
( context.focus.id = browser.contextMenus.create( context.focus.menu ));
storage.option.menu.read &&
( context.read.id = browser.contextMenus.create( context.read.menu ));
storage.option.menu.link &&
( context.link.id = browser.contextMenus.create( context.link.menu ));
storage.option.menu.list &&
( context.list.id = browser.contextMenus.create( context.list.menu ));
}
/**
* Create menu from type
*
* @param {string} include: foucs read link
*/
function create( type ) {
if ( !context[type].id ) {
delete context[type].menu.generatedId;
context[type].id = browser.contextMenus.create( context[type].menu );
}
}
/**
* Remove menu from type
*
* @param {string} include: foucs read link
*/
function remove( type ) {
if ( context[type].id ) {
browser.contextMenus.remove( context[type].id );
context[type].id = undefined;
}
}
/**
* Update menu from type
*
* @param {string} include: tempread and read
*/
function update( type ) {
if ( context.read.id ) {
const title = type == "read" ? "阅读模式" : "临时阅读模式";
browser.contextMenus.update( context.read.id, { title } );
}
}
/**
* Refresh menu ( Enforcement fresh )
*
* @param {object} new menu object
*/
function refresh( cur ) {
Object.keys( cur ).forEach( item => {
browser.runtime.sendMessage( msg.Add( msg.MESSAGE_ACTION.menu, { id: item, value: cur[item] } ));
});
}
export {
createAll as CreateAll,
create as Create,
remove as Remove,
update as Update,
refresh as Refresh,
onClicked as OnClicked,
}