forked from kellenmace/wp-admin-switcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
519 lines (446 loc) · 13.4 KB
/
content.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/**
* WordPress Admin Switcher Content Script
*/
(() => {
/**
* Receive message from background script.
*
* @param {Object} request The request (message).
*/
function receiveMessageFromBackgroundScript(request) {
if (request.toggle) {
toggleAdmin();
}
}
/**
* Switch to/from the WordPress Admin.
*/
function toggleAdmin() {
if (isWordPressAdmin(window.location.pathname)) {
window.location = getFrontEndUrl();
} else {
window.location = getAdminUrl();
}
}
/**
* Is this the WordPress admin?
*
* @param {string} url The URL to check.
* @return {boolean} Whether currently in the WordPress admin.
*/
function isWordPressAdmin(url) {
return url.indexOf("/wp-admin/") > -1 || url.indexOf("/wp-login.php") > -1;
}
/**
* Get frontend URL.
*
* @return {string} The frontend URL.
*/
function getFrontEndUrl() {
const urlFromAdminBar = getFrontEndUrlFromAdminBar();
if (urlFromAdminBar) {
return urlFromAdminBar;
}
const urlFromWindow = getFrontEndUrlFromWindowLocation();
if (urlFromWindow) {
return urlFromWindow;
}
// Return this as a last resort. Has the potential to
// be incorrect on subdirectory multisite installs.
return window.location.origin;
}
/**
* Get front end URL from admin bar.
*
* Try to get the link to view/preview the post first,
* then fallback to getting the main front end url.
*
* @return {string|boolean} The url or false on failure.
*/
function getFrontEndUrlFromAdminBar() {
const adminBarIds = {
view: "#wp-admin-bar-view",
preview: "#wp-admin-bar-preview",
siteName: "#wp-admin-bar-site-name",
};
return getUrlFromAdminBar(adminBarIds);
}
/**
* Get front end URL from window.location.
*
* @return {string|boolean} The url or false on failure.
*/
function getFrontEndUrlFromWindowLocation() {
const adminUrlParts = {
wpAdmin: "/wp-admin/",
wpLogin: "/wp-login.php",
};
for (let index in adminUrlParts) {
let adminUrlPartPosition = getPositionOfStringInCurrentUrl(
adminUrlParts[index]
);
const wasUrlFound = adminUrlPartPosition > -1;
if (wasUrlFound) {
return getPartOfUrlBeforePosition(adminUrlPartPosition);
}
}
return false;
}
/**
* Get the position of a string in the current URL.
*
* @param {string} string The string to find within the URL.
* @return {number} The position of string within the URL.
*/
function getPositionOfStringInCurrentUrl(string) {
return window.location.href.indexOf(string);
}
/**
* Get part of URL before the provided position.
*
* @param {number} stringPosition Exclude the character at this position and following.
* @return {string} The part of the URL before stringPosition.
*/
function getPartOfUrlBeforePosition(stringPosition) {
return window.location.href.substring(0, stringPosition);
}
/**
* Get the admin URL.
*
* @return {string} The admin URL.
*/
function getAdminUrl() {
const urlFromAdminBar = getAdminUrlFromAdminBar();
if (urlFromAdminBar) {
return urlFromAdminBar;
}
const urlFromPage = getAdminUrlFromPageSource();
if (urlFromPage) {
return urlFromPage;
}
// Return this as a last resort. Has the potential to
// be incorrect on subdirectory multisite installs.
return trailingSlashIt(window.location.origin) + "wp-admin/";
}
/**
* Get admin URL from page source.
*
* @return {string|boolean} The admin URL or false on failure.
*/
function getAdminUrlFromPageSource() {
let url = inferUrlFromPageLinks(),
postId = inferPostIdFromPageSource();
if (url) {
let adminUrl = getAdminUrlFromSiteUrl(url);
if (postId) {
adminUrl = getPostSpecificAdminUrl(adminUrl, postId);
}
return adminUrl;
}
return false;
}
/**
* Turn site URL into admin URL.
*
* @param {string} url The site URL.
* @return {string} The admin URL.
*/
function getAdminUrlFromSiteUrl(url) {
return trailingSlashIt(url) + "wp-admin/";
}
/**
* Turn base admin URL into a post-specific admin URL.
*
* @param {string} adminUrl The base admin URL.
* @param {string} postId The post ID.
* @return {string} The post-specific admin URL.
*/
function getPostSpecificAdminUrl(adminUrl, postId) {
return adminUrl + "post.php?post=" + postId + "&action=edit";
}
/**
* Get WP admin URL from the admin bar.
*
* @return {string|boolean} The URL or false on failure.
*/
function getAdminUrlFromAdminBar() {
const adminBarIds = {
edit: "#wp-admin-bar-edit",
siteName: "#wp-admin-bar-site-name",
};
return getUrlFromAdminBar(adminBarIds);
}
/**
* Get URL from the WP admin bar.
*
* @param {Object} adminBarIds The element IDs to use as selectors.
* @return {string|boolean} The URL or false on failure.
*/
function getUrlFromAdminBar(adminBarIds) {
if (isEmptyObject(adminBarIds)) {
return false;
}
for (let key in adminBarIds) {
let adminBarAnchorHtmlElement = getAdminBarAnchorHtmlElement(
adminBarIds[key]
);
if (wereHtmlElementsFound(adminBarAnchorHtmlElement)) {
return getHrefUrlFromHtmlElement(adminBarAnchorHtmlElement);
}
}
return false;
}
/**
* Is this an empty object?
*
* @param {Object} object The object to check.
* @return {boolean} Whether the object is empty.
*/
function isEmptyObject(object) {
return Object != object.constructor || Object.keys(object).length < 1;
}
/**
* Get admin bar anchor HTML element.
*
* @param {string} adminBarId The id of the HTML element to get.
* @return {Object} The matching HTML element NodeList.
*/
function getAdminBarAnchorHtmlElement(adminBarId) {
return document.querySelectorAll(adminBarId + " > a.ab-item[href]");
}
/**
* Here any HTML elements found?
*
* @param {Object} nodelist The NodeList of HTML elements found.
* @return {boolean} Whether HTML elements were found.
*/
function wereHtmlElementsFound(nodelist) {
return nodelist.length > 0;
}
/**
* Get the URL from an HTML element's href property.
*
* @param {Object} element The HTML element NodeList.
* @return {string} The URL.
*/
function getHrefUrlFromHtmlElement(element) {
return element[0].getAttribute("href");
}
/**
* Infer the site's URL from links in the page source.
*
* We can't simply get the site's URL from window.location
* because subdomain multisite installs need to have the
* /site-name/ part of the pathname preserved.
*
* @return {string|boolean} The URL or false on failure.
*/
function inferUrlFromPageLinks() {
const selectors = {
stylesheets: 'link[rel="stylesheet"][href]',
scripts: "script[src]",
rss: 'link[type="application/rss+xml"][href]',
xmlrpc: 'link[rel="pingback"][href]',
};
for (let selectorIndex in selectors) {
let elements = document.querySelectorAll(selectors[selectorIndex]);
if (wereHtmlElementsFound(elements)) {
for (let elementIndex in elements) {
let attribute = getAttributeForSelector(selectorIndex),
url = inferUrlFromPageLink(elements[elementIndex], attribute);
if (url) {
return url;
}
}
}
}
return false;
}
/**
* Extract the URL from an HTML element.
*
* @param {Object} element The HTML element
* @param {string} attribute The attribute to get the URL from: 'href' or 'src'.
* @return {string|boolean} The URL or false on failure.
*/
function inferUrlFromPageLink(element, attribute) {
const paths = {
wpContent: "/wp-content/",
wpIncludes: "/wp-includes/",
};
for (let index in paths) {
let url = getUrlSubstringFromHtmlElement(
element,
attribute,
paths[index]
);
if (url) {
return url;
}
}
return false;
}
/**
* Get the URL-containing attribute for a selector type.
*
* @param {string} selector The selector type.
* @return {string} The attribute for contains the URL.
*/
function getAttributeForSelector(selector) {
switch (selector) {
case "scripts":
return "src";
default:
return "href";
}
}
/**
* Extract part of a URL from an HTML element.
*
* @param {Object} element The HTML element.
* @param {string} attribute The attribute to get URL from ('href' or 'src').
* @param {string} string The text to search for and exclude, along with
* everything after it.
* @return {string|boolean} The part of the URL or false on failure.
*/
function getUrlSubstringFromHtmlElement(element, attribute, string) {
const stringPosition = element[attribute].indexOf(string);
const wasUrlFound = stringPosition > -1;
if (wasUrlFound) {
return element[attribute].substring(0, stringPosition);
}
return false;
}
/**
* Infer post ID from the page source.
*
* Not all WordPress sites will have this exposed.
*
* @return {string|boolean} The post ID or false on failure.
*/
function inferPostIdFromPageSource() {
let postId = false,
postIdSelectors = {
commentsForm: "#comment_post_ID[value]",
shortlink: 'link[rel="shortlink"][href]',
body: "body[class]",
};
for (let index in postIdSelectors) {
const postIdElement = document.querySelectorAll(postIdSelectors[index]);
if (!wereHtmlElementsFound(postIdElement)) {
continue;
}
switch (index) {
case "commentsForm":
postId = getPostIdFromCommentsForm(postIdElement);
break;
case "shortlink":
postId = getPostIdFromShortlink(postIdElement);
break;
case "body":
postId = getPostIdFromBodyClass(postIdElement);
}
}
return postId;
}
/**
* Get post ID from comments form.
*
* @param {Object} postIdElement The HTML element NodeList containing the post ID.
* @return {string} The post ID.
*/
function getPostIdFromCommentsForm(postIdElement) {
return postIdElement[0].value;
}
/**
* Get post ID from shortlink.
*
* @param {Object} postIdElement The HTML element NodeList containing the post ID.
* @return {string|boolean} The post ID or false on failure.
*/
function getPostIdFromShortlink(postIdElement) {
const shortlinkUrl = getHrefUrlFromHtmlElement(postIdElement);
if (!isShortlinkUsingWpMeService(shortlinkUrl)) {
const positionOfUrlTextBeforePostId =
getPositionOfUrlTextBeforePostId(shortlinkUrl);
const wasPostIdFound = positionOfUrlTextBeforePostId > -1;
if (wasPostIdFound) {
return getPostIdFromShortlinkUrl(
shortlinkUrl,
positionOfUrlTextBeforePostId
);
}
}
return false;
}
/**
* Is shortlink using wp.me service?
*
* @param {string} shortlinkUrl The shortlink URL.
* @return {boolean} Whether shorlinkUrl is using wp.me.
*/
function isShortlinkUsingWpMeService(shortlinkUrl) {
return shortlinkUrl.indexOf("//wp.me/") > -1;
}
/**
* Get the position of the URL before the post ID.
*
* @param {string} shortlinkUrl The URL to search.
* @return {number} The position of the post ID.
*/
function getPositionOfUrlTextBeforePostId(shortlinkUrl) {
return shortlinkUrl.indexOf("/?p=");
}
/**
* Extract the post ID from the shortlink URL.
*
* @param {string} shortlinkUrl The shortlink URL.
* @param {number} positionOfUrlTextBeforePostId The position of URL text before the post ID.
* @return {string} The post ID.
*/
function getPostIdFromShortlinkUrl(
shortlinkUrl,
positionOfUrlTextBeforePostId
) {
return shortlinkUrl.substring(
positionOfUrlTextBeforePostId + 4,
shortlinkUrl.length
);
}
/**
* Get post ID from body class.
*
* @param {Object} postIdElement The HTML element NodeList containing the post ID.
* @return {string|boolean} The post ID or false on failure.
*/
function getPostIdFromBodyClass(postIdElement) {
let bodyClasses = postIdElement[0].getAttribute("class"),
postIdPosition = bodyClasses.indexOf("postid-");
const wasPostIdFound = postIdPosition > -1;
if (wasPostIdFound) {
bodyClasses = bodyClasses.substring(
postIdPosition + 7,
bodyClasses.length
);
const firstSpacePosition = bodyClasses.indexOf(" ");
const wasFirstSpaceFound = firstSpacePosition > -1;
if (wasFirstSpaceFound) {
return bodyClasses.substring(0, firstSpacePosition);
}
return bodyClasses;
}
return false;
}
/**
* Add a trailing slash to a URL if it doesn't already have one.
*
* @param {string} url The URL.
* @return {string} url The URL with a trailing slash.
*/
function trailingSlashIt(url) {
if (url.endsWith("/")) return url;
return url + "/";
}
// Add event listener
chrome.runtime.onMessage.addListener(receiveMessageFromBackgroundScript);
})();