-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
executable file
·280 lines (225 loc) · 7.12 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
function getCurrentTabUrl(callback) {
chrome.tabs.query({
active: true,
currentWindow: true
}, (tabs) => {
let activeTab = tabs[0];
if ( typeof (activeTab) === 'undefined' || ! activeTab.url ) {
document.getElementById("report-handler").classList.add('hide');
document.getElementById("report-loader").classList.add('hide');
document.getElementById("report-warning").classList.remove('hide');
} else if (activeTab.url.match(/wp-admin(\/network)?\/(plugins|update-core)\.php/) == null) {
document.getElementById("report-handler").classList.add('hide');
document.getElementById("report-loader").classList.add('hide');
document.getElementById("report-warning").classList.remove('hide');
}
getResults(activeTab);
getAllResults(activeTab);
});
}
/**
* Listen for the content of the DOM to be DOMContentLoaded
* once complete, get the contents of the admin page
*/
document.addEventListener("DOMContentLoaded", function (event) {
getCurrentTabUrl();
});
let currentURL;
let data = {}
let dataAll = {}
/**
* Send a query to our content.js to get our list of WordPress update
* to a showResults callback that is used to populate popup.html
*/
const getResults = () => {
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
chrome.tabs.sendMessage(tabs[0].id, {action: "getWordPressUpdates"}, (response) => {
storeResults(response);
showResults(response);
chrome.browserAction.setBadgeText({text: (response.length).toString(), tabId: tabs[0].id});
});
});
}
const storeResults = ( response ) => {
if ( response ) {
data = response;
}
}
/**
* Send a query to our content.js to get our list of all Current WordPress versions
*/
const getAllResults = () => {
chrome.tabs.query({active: true, currentWindow: true}, (tabs) => {
chrome.tabs.sendMessage(tabs[0].id, {action: "getWordPressCurrent"}, (response) => {
storeAllResults(response);
});
});
}
const storeAllResults = ( response ) => {
if ( response ) {
dataAll = response;
}
}
/**
* Send the results/response to the popup.html
*/
const showResults = ( response ) => {
if ( response ) {
let jsonTable = json2table(response);
let resultsElement = document.getElementById("results");
resultsElement.innerHTML = jsonTable; // @todo should this be sanitized in any way?
let menu = document.createElement("control-menu");
menu.id = "control-menu";
menu.className = "control-menu";
resultsElement.append( menu );
document.getElementById("report-handler").classList.remove('hide');
document.getElementById("report-warning").classList.add('hide');
document.getElementById("report-loader").classList.add('hide');
buildControls();
}
}
/**
* Add a button to our controls
*
* @param label
* @param id
* @param className
* @param callback
*/
const addMenuButton = (label, id, className, callback ) => {
let button = document.createElement("button");
let buttonlabel = document.createTextNode(label);
button.className = className;
button.id = id;
button.appendChild( buttonlabel );
let controlMenu = document.getElementById('control-menu' );
controlMenu.appendChild( button );
document.getElementById( id ).addEventListener("click", callback );
}
/**
* Build our clipboard menu
*
* @return {HTMLElement}
*/
const buildControls = () => {
addMenuButton( 'Copy HTML Report', 'copy-html', 'button copy-html', copyHTMLReport );
addMenuButton( 'Copy Changelog Report', 'copy-report', 'button copy-report', copyChangelogReport );
addMenuButton( 'Copy Commit Report', 'copy-commit', 'button copy-commit', copyCommitReport );
addMenuButton( 'Copy Composer/Packagist', 'copy-cli', 'button copy-cli', copyComposerPackages );
addMenuButton( 'Copy Composer (Current)', 'copy-composer-current', 'button copy-composer-current', copyComposerCurrent );
}
/**
* Capitalize the first letter of a string. (We can probably do this with css)
*
* @param string
* @return {string}
*/
const capitalizeFirstLetter = ( string ) => {
return string.charAt(0).toUpperCase() + string.slice(1);
}
/**
* Format our results into a table
*
* @param json
* @param classes
* @return {string}
*/
const json2table = ( json, classes ) => {
if ( ! json[0] ) {
return 'Invalid Json';
}
let cols = Object.keys( json[0] );
let headerRow = '';
let bodyRows = '';
classes = classes || '';
cols.map( (col) => {
headerRow += '<th>' + capitalizeFirstLetter( col ) + '</th>';
});
json.map( ( row ) => {
bodyRows += '<tr>';
cols.map( ( colName) => {
bodyRows += '<td>' + row[ colName ] + '</td>';
} );
bodyRows += '</tr>';
} );
return '<table class="' +
classes +
'"><thead><tr>' +
headerRow +
'</tr></thead><tbody>' +
bodyRows +
'</tbody></table>';
}
/**
* Copy a .csv formatted report to the users clipboard
*/
const copyCSV = () => {
console.log('copy csv');
}
/**
* Copy a html formatted table of the report to the users clipboard
*/
const copyHTMLReport = () => {
copyToClipboard( json2table( data ) );
}
/**
* Copy a json formatted report for the changelog
*/
const copyChangelogReport = () => {
let bodyRows = '';
data.map( ( row ) => {
if ( ! row.plugin ) {
return
}
bodyRows += '* Updated "' + row.plugin + '" from version ' + row.currentVersion + ' to ' + row.nextVersion + "\n";
} );
copyToClipboard( bodyRows );
}
/**
* Copy for commit message
*/
const copyCommitReport = () => {
let bodyRows = '';
data.map( ( row ) => {
if ( ! row.plugin ) {
return
}
bodyRows += 'update(plugin): ' + row.plugin + ' (' + row.currentVersion + ' => ' + row.nextVersion + ") \n";
} );
copyToClipboard( bodyRows );
}
/**
* Copy a json formatted report to the users clipboard
*/
const copyComposerPackages = () => {
let bodyRows = '';
dataAll.map( ( row ) => {
if ( '' !== row.nextVersion ) {
bodyRows += '"wpackagist-plugin/' + row.slug + '": "' + row.nextVersion + '",' + "\n";
}
} );
copyToClipboard( bodyRows );
}
/**
* Copy a json formatted report to the users clipboard
*/
const copyComposerCurrent = () => {
let bodyRows = '';
dataAll.map( ( row ) => {
bodyRows += '"wpackagist-plugin/' + row.slug + '": "' + row.currentVersion + '",' + "\n";
} );
copyToClipboard( bodyRows );
}
/**
* Pass a string to copy to clipboard. Utilized by all copy buttons.
*
* @param str
*/
const copyToClipboard = str => {
const clipBoardTextArea = document.createElement( 'textarea' );
clipBoardTextArea.value = str;
document.body.appendChild( clipBoardTextArea );
clipBoardTextArea.select();
document.execCommand('copy');
document.body.removeChild( clipBoardTextArea );
};