-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
92 lines (83 loc) · 2.14 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
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
background = {
/**
* Data from the server
*/
data: {},
/**
* Initialization script
*/
init: function () {
this.bind();
// Get data from the server
let params = new URLSearchParams( {
action: 'askargs',
format: 'json',
formatversion: 2,
api_version: 2,
conditions: 'Type::Organization|Green_hosting::t',
printouts: 'Site|Green_hosting'
} );
fetch( "https://www.appropedia.org/w/api.php?" + params.toString() )
.then( response => response.json() )
.then( json => {
let results = json.query.results;
for ( let page in results ) {
let site = results[ page ].printouts.Site[0];
let url = new URL( site );
let domain = url.hostname.replace( 'www.', '' );
let green = results[ page ].printouts.Green_hosting[0];
if ( green === 't' ) {
background.data[ domain ] = 'green';
} else {
background.data[ domain ] = 'red';
}
}
} );
},
/**
* Bind events
*/
bind: function () {
chrome.tabs.onUpdated.addListener( this.onUpdated );
chrome.tabs.onActivated.addListener( this.onActivated );
},
/**
* Fires when a page is loaded or reloaded
*/
onUpdated: function( tabId, changeInfo, tab ) {
let color = background.getColor( tab.url );
background.updateIcon( tabId, color );
},
/**
* Fires when a tab is selected
*/
onActivated: function( activeInfo ) {
chrome.tabs.query( { active: true, currentWindow: true }, tabs => {
let tab = tabs[0];
let color = background.getColor( tab.url );
background.updateIcon( tab.id, color );
} );
},
/**
* Update the icon of the current tab
*/
updateIcon: function ( tabId, color ) {
let path = 'images/' + color + '19.png';
chrome.action.setIcon( { 'tabId': tabId, 'path': path } );
},
/**
* Get the color of the given URL
*/
getColor: function ( url ) {
let color = 'grey';
if ( url ) {
url = new URL( url );
let domain = url.hostname.replace( 'www.', '' );
if ( background.data[ domain ] ) {
color = background.data[ domain ];
}
}
return color;
}
};
background.init();