-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutomateController.js
187 lines (164 loc) · 6.02 KB
/
AutomateController.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
AutomateController = {
prepare: function(){
AutomateController.refresh();
jQuery('#refresh').click(function(){
AutomateController.refresh();
});
jQuery('#options').click(function(){
browser.runtime.openOptionsPage();
});
jQuery('#addAutomationButton').click(function(){
AutomateController.addAutomation();
});
},
save: function(){
AutomateModel.save().then(function(){
AutomateView.showMessageInfo('Data saved in the storage.');
});
},
refresh: function(){
AutomateModel.load().then(function(automations){
AutomateView.refresh(automations);
AutomateView.showMessageInfo('Data reloaded from the storage.');
});
},
// TODO: to remove
importFromFile: function(){
// TODO: ask confirmation because the automations will be overwritten
let filename = jQuery('#importFileInput').val().replace('C:\\fakepath\\', '');
if (!filename) {
AutomateView.showMessageError('Select a filename');
} else {
let path = jQuery('#importFilePathInput').val() + '/' + filename;
if (!path.startsWith('file://')) {
path = 'file://' + path;
}
fetch(path, {mode:'same-origin'}) // <-- important
.then(function(result){
return result.blob();
})
.then(function(blob){
let reader = new FileReader();
reader.addEventListener('loadend', function(){
AutomateModel.setAutomations(JSON.parse(this.result));
AutomateModel.save();
AutomateView.refresh(AutomateModel.getAutomations());
});
reader.readAsText(blob);
});
}
},
addAutomation: function(){
let automation = new Automation();
AutomateView.addAutomation(automation);
AutomateView.setEditMode(automation.guid);
},
deleteAutomation: function(guid){
if(confirm('Are you sure to delete this automation?')){
AutomateModel.deleteAutomation(guid);
AutomateModel.save().then(function(){
AutomateView.deleteAutomation(guid);
AutomateView.showMessageInfo('Automation successfully removed.');
});
}
},
editAutomation: function(guid){
if (!jQuery('#' + guid + ' .editAutomationButton').hasClass('md-inactive')) {
AutomateView.setEditMode(guid);
}
},
saveAutomation: function(guid){
let automationNew = AutomateView.saveAutomation(guid);
AutomateModel.saveAutomation(automationNew);
AutomateModel.save().then(function(){
AutomateView.setViewMode(guid);
AutomateView.showMessageInfo('Automation successfully saved.');
});
},
cancelEditAutomation: function(guid){
let automation = AutomateModel.getAutomation(guid);
if (automation == undefined) {
AutomateView.deleteAutomation(guid);
AutomateView.enableEdit();
} else {
AutomateView.addAutomation(automation);
AutomateView.setViewMode(guid);
AutomateView.showMessageDebug('Automation\'s edits cancelled.');
}
},
runAutomation: function(guid){
let automation = AutomateModel.getAutomation(guid);
Automation.run(automation)
.then(function(){
AutomateView.notify('Automation "' + automation.name + '" ended.');
AutomateView.showMessageDebug('Automation ended.');
}, function(){
AutomateView.notify('Automation "' + automation.name + '" ended with errors.');
AutomateView.showMessageError('Automation ended with errors.');
});
AutomateView.showMessageDebug('Automation is running...');
},
runAutomationFrom: function(automationGuid, actionGuid){
let automation = AutomateModel.getAutomation(automationGuid);
Automation.runFrom(automation, actionGuid)
.then(function(){
AutomateView.notify('Automation "' + automation.name + '" ended.');
AutomateView.showMessageDebug('Automation ended.');
}, function(){
AutomateView.notify('Automation "' + automation.name + '" ended with errors.');
AutomateView.showMessageError('Automation ended with errors.');
});
AutomateView.showMessageDebug('Automation is running...');
},
runAction: function(automationGuid, actionGuid){
let action = AutomateModel.getAction(automationGuid, actionGuid);
Action.run(action)
.then(function(){
AutomateView.notify('Action "' + action.actionType + '" ended.');
AutomateView.showMessageDebug('Action ended.');
}, function(){
AutomateView.notify('Action "' + action.name + '" ended with errors.');
AutomateView.showMessageError('Action ended with errors.');
});
AutomateView.showMessageDebug('Action is running...');
},
saveAutomationsOrder: function(){
let automationsGuid = AutomateView.getAutomationsOrder();
AutomateModel.orderAutomations(automationsGuid);
AutomateModel.save().then(function(){
AutomateView.showMessageDebug('Sorting ended!!!');
});
},
addAction: function(guid){
let action = new Action();
AutomateView.addAction(guid, action);
AutomateView.setEditMode(guid);
},
deleteAction: function(automationGuid, actionGuid){
if(confirm('Are you sure to delete this action?')){
AutomateView.deleteAction(automationGuid, actionGuid);
AutomateView.showMessageInfo('Action successfully removed.');
}
},
pickElement: function(tags){
let p = browser.tabs.executeScript({
file: '/lib/css-selector-generator.min.js'
}).then(function(){
return browser.tabs.executeScript({
file: '/content_scripts/pick_element.js'
});
}).then(function(){
return browser.tabs.executeScript({
code: 'admittedTags = ' + JSON.stringify(tags) + ';'
});
}).then(function(){
let pp = new Promise(function(resolve, reject){
browser.runtime.onMessage.addListener(function (message) {
resolve(message);
});
});
return pp;
});
return p
},
};