forked from violentmonkey/violentmonkey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
136 lines (136 loc) · 3.82 KB
/
popup.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
var $=document.getElementById.bind(document),P=$('popup'),C=$('commands'),
pT=P.querySelector('.top'),pB=P.querySelector('.bot'),
cT=C.querySelector('.top'),cB=C.querySelector('.bot'),
tab=null,ia=null,scripts={},hr=null;
function loadItem(d,c) {
if(d.data=c){
d.firstChild.innerText=d.symbol;
d.classList.remove('disabled');
} else {
d.firstChild.innerText = '';
d.classList.add('disabled');
}
return c;
}
function addItem(h,c,b) {
var d=document.createElement('div');
d.innerHTML='<span></span>'+h;
if('title' in c) {
d.title=typeof c.title=='string'?c.title:h;
delete c.title;
}
d.className='ellipsis';
c.holder.insertBefore(d,b);
if('symbol' in c) d.firstChild.innerText = c.symbol;
for(h in c) d[h]=c[h];
return d;
}
function menuCommand(e) {
chrome.tabs.sendMessage(tab.id,{cmd:'Command',data:e.target.cmd});
}
function menuScript(s) {
if(scripts[s.id]) return;scripts[s.id]=s;
var n=s.meta.name?s.meta.name.replace(/&/g,'&').replace(/</g,'<'):'<em>'+_('labelNoName')+'</em>';
loadItem(addItem(n,{
holder: pB,
symbol: '✓',
title: s.meta.name,
onclick: function(e){
chrome.runtime.sendMessage({cmd:'UpdateMeta',data:{id:s.id,enabled:loadItem(this,!this.data)?1:0}});
}
}),s.enabled);
}
function initMenu(){
addItem(_('menuManageScripts'),{
holder: pT,
symbol: '➤',
//title: true,
onclick: function(){
var u=chrome.extension.getURL('/options.html');
chrome.tabs.query({currentWindow:true,url:u},function(t) {
if(t[0]) chrome.tabs.update(t[0].id,{active:true});
else chrome.tabs.create({url:u});
});
}
});
if(/^https?:\/\//i.test(tab.url)) {
var d=addItem(_('menuFindScripts'), {
holder: pT,
symbol: '➤',
//title: true,
});
loadItem(d,false);
chrome.runtime.sendMessage({cmd:'GetOption',data:'search'},function(o){
d.onclick=function(){
var q='site:userscripts.org+inurl:show+'+tab.url.replace(/^.*?:\/\/([^\/]*?)\.\w+\/.*$/, function(v,g){
return g.replace(/\.(com|..)$/, '').replace(/\./g, '+');
});
chrome.tabs.create({url:o.replace('*',q)});
};
loadItem(d,true);
});
}
ia=addItem(_('menuScriptEnabled'), {
holder: pT,
symbol: '✓',
//title: true,
onclick: function(e) {
chrome.runtime.sendMessage({cmd:'SetOption',data:{key:'isApplied',value:loadItem(this,!this.data)}});
chrome.browserAction.setIcon({path:'images/icon19'+(this.data?'':'w')+'.png'});
}
});
chrome.runtime.sendMessage({cmd:'GetOption',data:'isApplied'},function(o){loadItem(ia,o);});
}
function load(data) {
if(data&&data[0]&&data[0].length) {
addItem(_('menuBack'), {
holder: cT,
symbol: '◄',
//title: true,
onclick: function() {
C.classList.add('hide');
P.classList.remove('hide');
}
});
cT.appendChild(document.createElement('hr'));
data[0].forEach(function(i) {
addItem(i[0], {
holder: cB,
symbol: '➤',
//title: true,
onclick: menuCommand,
cmd: i[0]
});
});
addItem(_('menuCommands'),{
holder: pT,
symbol: '➤',
//title: true,
onclick: function() {
P.classList.add('hide');
C.classList.remove('hide');
}
},ia);
}
if(data&&data[1]&&data[1].length) {
var ids=[];
data[1].forEach(function(i){
if(!scripts[i]) ids.push(i);
});
if(ids.length) chrome.runtime.sendMessage({cmd:'GetMetas',data:ids},function(o){
if(!hr) pT.appendChild(hr=document.createElement('hr'));
o.forEach(menuScript);
});
}
}
chrome.runtime.onMessage.addListener(function(req,src,callback) {
var maps={
SetPopup: load,
},f=maps[req.cmd];
if(f) f(req.data,src,callback);
return true;
});
chrome.tabs.query({currentWindow:true,active:true},function(t) {
tab=t[0];initMenu();chrome.tabs.sendMessage(tab.id,{cmd:'GetPopup'});
});
initCSS();