-
Notifications
You must be signed in to change notification settings - Fork 2
/
background.js
64 lines (59 loc) · 1.89 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
60
61
62
63
64
//
// The background process
//
/*
Puts the button in toolbar and setups the popup and the badge
*/
var theButton;
window.addEventListener("load", function() {
var UIItemProperties = {
disabled: false,
title: getMsg("extension.name"),
icon: "assets/icons/icon_18.png",
popup: {
href: "popup.html",
width: 600,
height: 544
},
badge: {
textContent: '',
backgroundColor: '#E00',
color: '#FFF',
display: 'block'
}
}
theButton = opera.contexts.toolbar.createItem(UIItemProperties);
opera.contexts.toolbar.addItem(theButton);
// Enable the badge when an injected tab is detected
//opera.extension.onconnect = toggleBadge; // I'll do it in the DOMContentLoaded event, to allow both messaging and the badge in the onconnect event.
opera.extension.tabs.onfocus = toggleBadge;
opera.extension.tabs.onblur = toggleBadge;
}, false);
function toggleBadge() {
var tab = opera.extension.tabs.getFocused();
var vipUrlRegexp = /^https?:\/\/(articulo|produto)\.mercadoli(b|v)re\.(com\.ar\/MLA|com\.br\/MLB|cl\/MLC|com\.co\/MCO|com\.mx\/MLM|com\.ve\/MLV)-[0-9]+/;
if (tab && vipUrlRegexp.test(tab.url)) {
// http://www.alanwood.net/unicode/miscellaneous_symbols.html
theButton.badge.textContent = ' \u2605 '; // A nice star
}
else {
theButton.badge.textContent = '';
}
}
/*
Messaging setup to allow communication between the popup and the injected script.
Shamelessly copied from:
http://dev.opera.com/articles/view/opera-extensions-messaging/#popup_injectedscript
*/
window.addEventListener("DOMContentLoaded", function() {
opera.extension.onconnect = function(event) {
toggleBadge(); // Puts the bagde
if (event.origin.indexOf("popup.html") > -1 && event.origin.indexOf('widget://') > -1) {
var tab = opera.extension.tabs.getFocused();
if (tab) {
tab.postMessage("Send a port", [event.source]);
opera.postError('sent a message to injected script');
}
}
}
}, false);