-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwp-admin-su-fix.user.js
More file actions
92 lines (82 loc) · 3.36 KB
/
wp-admin-su-fix.user.js
File metadata and controls
92 lines (82 loc) · 3.36 KB
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
// ==UserScript==
// @name WP-Admin SU Fix
// @namespace wp-admin-su-fix
// @namespace http://wordpress.com/
// @version 0.3
// @description Redirect to the clicked wp-admin panel link after SU.
// @updateURL https://github.com/druesome/andrew-scripts/raw/main/wp-admin-su-fix.user.js
// @downloadURL https://github.com/druesome/andrew-scripts/raw/main/wp-admin-su-fix.user.js
// @author druesome
// @match https://wordpress.com/*
// @match https://*/wp-admin/
// @grant GM_setValue
// @grant GM_getValue
// @run-at document-idle
// ==/UserScript==
(function() {
console.log('Script started.');
function isAdminPage() {
return document.body.classList.contains('wp-admin');
}
function handleAdminLinks() {
var adminLinks = document.querySelectorAll('a[href*="/wp-admin/"]');
console.log('Found ' + adminLinks.length + ' admin links.');
adminLinks.forEach(function(link) {
link.addEventListener('click', function(e) {
e.preventDefault();
var targetUrl = this.getAttribute('href');
console.log('Admin link clicked. Target URL:', targetUrl);
GM_setValue('postSSORedirect', targetUrl);
console.log('Tampermonkey Storage - Saved Redirect URL:', GM_getValue('postSSORedirect'));
location.replace(targetUrl);
});
});
}
function handleAdditionalLinks() {
var additionalLinks = document.querySelectorAll('a[href*="/post/"], a[href*="/page/"]');
console.log('Found ' + additionalLinks.length + ' additional links.');
additionalLinks.forEach(function(link) {
link.addEventListener('click', function(e) {
e.preventDefault();
var targetUrl = "https://wordpress.com" + this.getAttribute('href');
console.log('Additional link clicked. Target URL:', targetUrl);
GM_setValue('postSSORedirect', targetUrl);
console.log('Tampermonkey Storage - Saved Redirect URL:', GM_getValue('postSSORedirect'));
location.replace(targetUrl);
});
});
}
function onDomContentLoaded() {
console.log('DOM fully loaded.');
if (isAdminPage()) {
console.log('We are on an admin page.');
const postSSORedirect = GM_getValue('postSSORedirect');
console.log('Saved Redirect URL:', postSSORedirect);
if (postSSORedirect) {
console.log('Redirecting to saved URL.');
GM_setValue('postSSORedirect', null);
location.replace(postSSORedirect);
}
} else {
handleAdminLinks();
handleAdditionalLinks();
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.addedNodes && mutation.addedNodes.length > 0) {
handleAdminLinks();
handleAdditionalLinks();
}
});
});
observer.observe(document.body, {
childList: true,
subtree: true
});
}
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', onDomContentLoaded);
} else {
onDomContentLoaded();
}
})();