-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.js
291 lines (197 loc) · 6.66 KB
/
scraper.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
281
282
283
284
285
286
287
288
289
290
291
/*
Program Name: Console: List WordPress plugins and description
File Name: ehCode_2019.02.14_JavaScriptES6_ListWordPresPluginsAdmin_01.00.js
Date Created: 02/14/18
Date Modified: --
Version: 1.00
Programmer: Eric Hepperle
Purpose: Scrape plugin information using vanilla JavaScript ES6
and then display as pipe-separated or as html table.
Usage:
- Login to WordPress dashboard and navigate to Plugins page
- Paste the code below into browser console and hit enter
- Copy-paste either pipe-separated list into a spreadsheet and save,
or HTML table into a .htm/.html file, save and launch in browser.
Returns:
A list of installed plugin names and associated data formatted 2 ways:
pipe-separated and HTML tables.
Sample Results:
[Pipe-Separated]
1 | Advanced Custom Fields PRO | Customize WordPress with powerful, professional and intuitive fields. | 5.7.10 | Elliot Condon |
2 | bbPress | bbPress is forum software with a twist from the creators of WordPress. | 2.5.14 | The bbPress Community |
3 | Contact Form 7 | Just another contact form plugin. Simple but flexible. | 5.1.1 | Takayuki Miyoshi |
...
[HTML Table]
<table>
<tr>
<td>1</td>
<td>Advanced Custom Fields PRO</td>
<td>Customize WordPress with powerful, professional and intuitive fields.</td>
<td>5.7.10</td>
<td><a href="http://www.elliotcondon.com/">Elliot Condon</a></td>
</tr>
<tr>
<td>2</td>
<td>Akismet Anti-Spam</td>
<td>Used by millions, Akismet is quite possibly the best way in the world to protect your blog from spam. Your site is fully configured and being protected, even while you sleep.</td>
<td>4.1</td>
<td><a href="https://automattic.com/wordpress-plugins/">Automattic</a></td>
</tr> ...
Requires:
* WordPress login.
* Installed plugins.
Demonstrates:
- ES6
- Vanilla JavaScript
- Spread Operator
- forEach()
- querySelectorAll()
- DOM traversal
- Pseudocode Algorithm
*/
/* global $ */
/*jshint esversion: 6 */
console.clear();
/* ----------- VARIABLES ----------- */
// Create an array to hold processed plugin group objects
var groupsArr = [];
var outputViews = {}; // object with string properties
var selPluginGroups = '#the-list tr.active:not(.plugin-update-tr)';
var selPluginTitle = '.plugin-title > strong';
var selPluginDescr = '.plugin-description';
var selPluginVerAuthURI = '.plugin-version-author-uri';
var pluginGroups = document.querySelectorAll(selPluginGroups);
console.log('pluginGroups');
console.log(pluginGroups);
// --- POPULATE GROUPS ARRAY --- //
Array.from(pluginGroups).forEach(function(group, i) {
var thisObj = {};
var pluginTitle = group.querySelector(selPluginTitle).innerText;
var pluginDescr = group.querySelector(selPluginDescr).innerText;
var authVerURI_txt = group.querySelector(selPluginVerAuthURI).innerText.split(' | ');
var authVerURI_htm = group.querySelector(selPluginVerAuthURI).innerHTML.split(' | ');
var pluginVersion_txt = authVerURI_txt[0].split('Version ')[1];
var pluginVersion_htm = authVerURI_htm[0].split('Version ')[1];
var pluginAuthor_txt = authVerURI_txt[1].split('By ')[1];
var pluginAuthor_htm = authVerURI_htm[1].split('By ')[1];
// Add properties to current object
thisObj.pluginTitle = pluginTitle;
thisObj.pluginDescr = pluginDescr;
thisObj.pluginVersion_txt = pluginVersion_txt;
thisObj.pluginAuthor_txt = pluginAuthor_txt;
thisObj.pluginVersion_htm = pluginVersion_htm;
thisObj.pluginAuthor_htm = pluginAuthor_htm;
groupsArr.push(thisObj);
});
console.log('groupsArr');
console.log(groupsArr);
// --- FORMAT PLUGINS LIST AS PIPE-SEPARATED --- //
var outStr = "";
var count = 1;
console.log("GROUPS ARRAY");
groupsArr.forEach(function(group, i) {
console.log(group);
var pluginInfoStr =
count + " | "
+ group.pluginTitle + " | "
+ group.pluginDescr + " | "
+ group.pluginVersion_txt + " | "
+ group.pluginAuthor_txt + " | ";
outStr += pluginInfoStr + "\n";
count++;
});
console.log('OUT STRING');
console.log(outStr);
outputViews.psv = outStr;
console.log('outputViews');
console.log(outputViews);
// --- FORMAT PLUGINS LIST AS HTML --- //
var outStr = "<table>\n";
var count = 1;
console.log("HTML OUTPUT");
groupsArr.forEach(function(group, i) {
console.log(group);
var pluginInfoStr =
"\t<tr>\n" +
"\t\t<td>"
+ count +
"</td>\n" +
"\t\t<td>"
+ group.pluginTitle +
"</td>\n" +
"\t\t<td>"
+ group.pluginDescr +
"</td>\n" +
"\t\t<td>"
+ group.pluginVersion_htm +
"</td>\n" +
"\t\t<td>"
+ group.pluginAuthor_htm +
"</td>\n" +
"\t</tr>\n";
outStr += pluginInfoStr;
count++;
});
// close table
outStr += "<table>";
console.log('OUT STRING');
console.log(outStr);
outputViews.htm = outStr;
console.log('outputViews');
console.log(outputViews);
/* ----------- FUNCTIONS ----------- */
/*
@params:
- l = label; text to identify the object we are printing
- o = the object to be displayed
- s = any formatting for the label
*/
function logObj(l, o, s) {
console.log("%c\t\t%s\t\t",s,l);
console.log(o);
}
/*
Note: this regex is pulled from:
https://stackoverflow.com/questions/45380207/regex-to-extract-the-top-level-domain-from-a-url
http://www.golangprograms.com/regular-expression-to-extract-domain-from-url.html
*/
function getDomain(u){
var url = u;
var reg = new RegExp("^(?:https?:\/\/)?(?:[^@\/\n]+@)?(?:www\.)?([^:\/\n]+)");
var domain = url.match(reg)[1];
return domain;
}
/*
NOTES:
02/14/19 - Created file. WORKS!
*/
/*
ALGORITHM:
Create an array to hold processed plugin group objects
DEFINE VARIABLES:
- Plugin group selector
CREATE & POPULATE GROUPS ARRAY
Grab all PLUGIN GROUPS with querySelectorAll()
For each PLUGIN GROUP
CREATE new plugin group object
Foreach group object
Grab plugin name
Add plugin name as object property
Grab Description
Addd description as object property
Grab Version
Add version as object property
Grab Author
Add autho as property
ADD plugin group object to groups array
CREATE FORMATTED Pipe-Separated OUTPUT
Create Out string variable
Initialize to ""
Create count variable
Initialize count to 1
Foreach group in groups array
Current plugin info string = count | pluginName | description | version | author
Append plugin info string to out string
Increment count
Return out string to console
*/