-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpopup.js
109 lines (88 loc) · 2.02 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
var bp = chrome.extension.getBackgroundPage();
var MainModel = Backbone.Model.extend({
defaults: {
activePage: null,
running: false,
ended: false
},
actions: {
followers: {
run: bp.startFollowing,
stop: bp.stopFollowing
},
following: {
run: bp.startUnfollowing,
stop: bp.stopUnfollowing
}
},
run: function() {
this.actions[this.get('activePage')].run();
this.set('running', true);
},
stop: function() {
this.actions[this.get('activePage')].stop();
this.set('running', false);
}
});
var MainView = Backbone.View.extend({
el: "#action-container",
events: {
"click #action-btn": "onActionClicked"
},
actions: {
followers: {
initial: 'Start Following',
running: 'Stop Following',
ended: 'Ended'
},
following: {
initial: 'Start Unfollowing',
running: 'Stop Unfollowing',
ended: 'Ended'
}
},
initialize: function() {
this.listenTo(this.model, "change:running change:ended", this.render, this);
},
onActionClicked: function() {
// if script has ended, do nothing
if (this.model.get('ended')) {
return;
}
// Stop action
if (this.model.get('running')) {
this.model.stop();
} else { // Run action
this.model.run();
}
},
getState: function() {
if (this.model.get('ended')) {
return 'ended';
}
if (this.model.get('running')) {
return 'running';
}
return 'initial';
},
render: function() {
var text = this.actions[this.model.get('activePage')][this.getState()];
$('#action-btn').text(text);
}
});
function startup(activePage) {
var mm = new MainModel({activePage: activePage}),
mv = new MainView({model: mm});
bp.isRunning(function(isRunning) {
console.log('isRunning', isRunning);
mm.set('running', isRunning);
});
mv.render();
bp.addListener('ended', function() {
mm.set({
running: false,
ended: true
});
});
}
window.onload = bp.executeWhenReady(startup);