Skip to content
This repository was archived by the owner on Jul 27, 2024. It is now read-only.

Commit 31aa20c

Browse files
committed
Merge branch 'dev'
2 parents e325c1a + ee8a751 commit 31aa20c

6 files changed

Lines changed: 160 additions & 10 deletions

File tree

src/background/app.js

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var badges = require('./badges');
55
var cache = require('./utils/cache');
66
var tabsUtils = require('./utils/tabs');
77
var scriptUtils = require('./utils/script');
8+
var clipboard = require('./utils/clipboard');
89
var _ = require('../common');
910

1011
var vmdb = exports.vmdb = new VMDB;
@@ -92,7 +93,16 @@ var commands = {
9293
});
9394
},
9495
SetValue: function (data, _src) {
95-
return vmdb.setValue(data.uri, data.values);
96+
return vmdb.setValue(data.uri, data.values)
97+
.then(function () {
98+
tabsUtils.broadcast({
99+
cmd: 'UpdateValues',
100+
data: {
101+
uri: data.uri,
102+
values: data.values,
103+
},
104+
});
105+
});
96106
},
97107
GetOptions: function (_data, _src) {
98108
return _.options.getAll();
@@ -184,8 +194,37 @@ var commands = {
184194
GetFromCache: function (data, _src) {
185195
return cache.get(data) || null;
186196
},
197+
Notification: function (data, _src) {
198+
return notification(data);
199+
},
200+
SetClipboard: function (data, _src) {
201+
clipboard.set(data);
202+
},
187203
};
188204

205+
var notification = function () {
206+
function notification(data) {
207+
var n = new Notification(data.title || _.i18n('extName'), {
208+
body: data.text,
209+
icon: data.image,
210+
});
211+
var nid = ++ id;
212+
n.onclick = wrapEvent(nid, 'NotificationClick');
213+
n.onclose = wrapEvent(nid, 'NotificationClose');
214+
return nid;
215+
}
216+
function wrapEvent(nid, evt) {
217+
return function () {
218+
tabsUtils.broadcast({
219+
cmd: evt,
220+
data: nid,
221+
});
222+
};
223+
}
224+
var id = 0;
225+
return notification;
226+
}();
227+
189228
function reinit() {
190229
var func = function (f) {
191230
var c = 0;

src/background/utils/clipboard.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var textarea = document.createElement('textarea');
2+
document.body.appendChild(textarea);
3+
4+
var clipboardData = {};
5+
function oncopy(e) {
6+
e.preventDefault();
7+
e.clipboardData.setData(clipboardData.type || 'text/plain', clipboardData.data);
8+
}
9+
document.addEventListener('copy', oncopy, false);
10+
11+
exports.set = function (data) {
12+
clipboardData.type = data.type;
13+
clipboardData.data = data.data;
14+
textarea.focus();
15+
document.execCommand('copy', false, null);
16+
};

src/background/utils/tabs.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ var on = function () {
3434
return register;
3535
}();
3636

37+
function broadcast(data) {
38+
_.mx.rt.post('Broadcast', data);
39+
}
40+
3741
module.exports = Object.assign(_.tabs, {
3842
update: update,
3943
on: on,
44+
broadcast: broadcast,
4045
});

src/injected.js

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function post(target, data, callback) {
6363
}
6464
rt.post(target, data);
6565
}
66-
rt.listen(id, function (obj) {
66+
function onMessage(obj) {
6767
var maps = {
6868
Command: function (data) {
6969
comm.post({cmd: 'Command', data: data});
@@ -77,10 +77,14 @@ rt.listen(id, function (obj) {
7777
HttpRequested: function (res) {
7878
comm.post({cmd: 'HttpRequested', data: res});
7979
},
80+
NotificationClick: onNotificationClick,
81+
NotificationClose: onNotificationClose,
8082
};
8183
var func = maps[obj.cmd];
8284
if (func) func(obj.data);
83-
});
85+
}
86+
rt.listen(id, onMessage);
87+
rt.listen('Broadcast', onMessage);
8488

8589
/**
8690
* @desc Wrap methods to prevent unexpected modifications.
@@ -210,6 +214,25 @@ var comm = {
210214
var req = comm.requests[r.id];
211215
if (req) req.callback(r);
212216
},
217+
UpdateValues: function (data) {
218+
var values = comm.values;
219+
if (values && values[data.uri]) values[data.uri] = data.values;
220+
},
221+
NotificationClicked: function (id) {
222+
var options = comm.notif[id];
223+
if (options) {
224+
var onclick = options.onclick;
225+
onclick && onclick();
226+
}
227+
},
228+
NotificationClosed: function (id) {
229+
var options = comm.notif[id];
230+
if (options) {
231+
delete comm.notif[id];
232+
var ondone = options.ondone;
233+
ondone && ondone();
234+
}
235+
},
213236
};
214237
var func = maps[obj.cmd];
215238
if (func) func(obj.data);
@@ -501,6 +524,41 @@ var comm = {
501524
return comm.Request(details);
502525
},
503526
},
527+
GM_notification: {
528+
value: function (text, title, image, onclick) {
529+
if (!text) {
530+
throw 'Invalid parameters.';
531+
}
532+
var options = typeof text === 'object' ? text : {
533+
text: text,
534+
title: title,
535+
image: image,
536+
onclick: onclick,
537+
};
538+
var id = comm.notif[''] = (comm.notif[''] || 0) + 1;
539+
comm.notif[id] = options;
540+
comm.post({
541+
cmd: 'Notification',
542+
data: {
543+
id: id,
544+
text: options.text,
545+
title: options.title,
546+
image: options.image,
547+
},
548+
});
549+
},
550+
},
551+
GM_setClipboard: {
552+
value: function (text, type) {
553+
comm.post({
554+
cmd: 'SetClipboard',
555+
data: {
556+
type: type,
557+
data: text,
558+
},
559+
});
560+
},
561+
},
504562
};
505563
comm.forEach(grant, function (name) {
506564
var prop = gm_funcs[name];
@@ -551,6 +609,7 @@ var comm = {
551609
var idle = [];
552610
var end = [];
553611
comm.command = {};
612+
comm.notif = {};
554613
comm.version = data.version;
555614
comm.values = {};
556615
// reset load and checkLoad
@@ -616,11 +675,33 @@ function handleC(e) {
616675
document.head.appendChild(style);
617676
}
618677
},
678+
Notification: onNotificationCreate,
679+
SetClipboard: function (data) {
680+
post('Background', {cmd: 'SetClipboard', data: data});
681+
},
619682
};
620683
var func = maps[req.cmd];
621684
if (func) func(req.data);
622685
}
623686

687+
var notifications = {};
688+
function onNotificationCreate(options) {
689+
post('Background', {cmd: 'Notification', data: options}, function (nid) {
690+
notifications[nid] = options.id;
691+
});
692+
}
693+
function onNotificationClick(nid) {
694+
var id = notifications[nid];
695+
id && comm.post({cmd: 'NotificationClicked', data: id});
696+
}
697+
function onNotificationClose(nid) {
698+
var id = notifications[nid];
699+
if (id) {
700+
comm.post({cmd: 'NotificationClosed', data: id});
701+
delete notifications[nid];
702+
}
703+
}
704+
624705
function objEncode(obj) {
625706
var list = [];
626707
for (var i in obj) {

src/popup/views/item.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
<div class="menu-item" :class="[options.className,{disabled:options.disabled}]" :title="options.title||options.name">
2-
<div class="menu-item-detail" v-if="options.detailClick" @click="detailClick">...</div>
1+
<div class="menu-item" :class="[data.className,{disabled:data.disabled}]" :title="data.title||data.name">
2+
<div class="menu-item-detail" v-if="data.detailClick" @click="detailClick">...</div>
33
<div class="menu-item-label" @click="onClick">
4-
<svg class="icon"><use :xlink:href="'#'+options.symbol"/></svg>
5-
{{options.name}}
4+
<svg class="icon"><use :xlink:href="'#'+data.symbol"/></svg>
5+
{{data.name}}
66
</div>
77
</div>

src/popup/views/item.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,23 @@ module.exports = {
1313
props: ['options'],
1414
template: cache.get('./item.html'),
1515
data: function () {
16-
// make options reactive
1716
return {
18-
reactiveOptions: this.options,
17+
data: {},
1918
};
2019
},
20+
watch: {
21+
options: 'update',
22+
},
2123
methods: {
24+
update: function () {
25+
this.data = this.options;
26+
this.init();
27+
},
28+
init: wrapHandler('init'),
2229
onClick: wrapHandler('onClick'),
2330
detailClick: wrapHandler('detailClick'),
2431
},
25-
mounted: wrapHandler('init'),
32+
mounted: function () {
33+
this.update();
34+
},
2635
};

0 commit comments

Comments
 (0)