forked from jae-jae/Userscript-Plus
-
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathstart.js
More file actions
172 lines (155 loc) · 4.52 KB
/
Copy pathstart.js
File metadata and controls
172 lines (155 loc) · 4.52 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
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
'use strict';
import { log, info, err } from './logger.js';
import { dom, qs, SafeAnimationFrame } from './querySelector.js';
let cfg = {};
const messenger = userjs.hermes.getPort();
const sleazyRedirect = () => {
if (!/greasyfork\.org/.test(window.location.hostname) && cfg.sleazyredirect) {
return;
}
const otherSite = /greasyfork\.org/.test(window.location.hostname) ? 'sleazyfork' : 'greasyfork';
qs('span.sign-in-link')
? /scripts\/\d+/.test(window.location.href)
? !qs('#script-info') && (otherSite == 'greasyfork' || qs('div.width-constraint>section>p>a'))
? window.location.assign(
window.location.href.replace(
/\/\/([^.]+\.)?(greasyfork|sleazyfork)\.org/,
'//$1' + otherSite + '.org'
)
)
: false
: false
: false;
};
const Container = class {
constructor() {
this.remove = this.remove.bind(this);
this.onFrameLoad = this.onFrameLoad.bind(this);
this.ready = false;
this.supported = userjs.isFN(document.createElement('main-userjs').attachShadow);
if (this.supported) {
this.frame = userjs.make('main-userjs', '', {
dataset: {
insertedBy: 'userscript-plus',
role: 'primary-container'
}
});
/**
* @type { ShadowRoot }
*/
this.root = this.frame.attachShadow({ mode: 'open' });
this.ready = true;
} else {
this.frame = userjs.make('iframe', 'mujs-iframe', {
dataset: {
insertedBy: 'userscript-plus',
role: 'primary-iframe'
},
loading: 'lazy',
src: 'about:blank',
style:
'position: fixed;bottom: 1rem;right: 1rem;height: 525px;width: 90%;margin: 0px 1rem;z-index: 100000000000000020 !important;',
onload: this.onFrameLoad
});
}
userjs.ael(window.self, 'beforeunload', this.remove);
// dbg('Container:', this);
}
/**
* @param { Function } callback
* @param { document } doc
*/
async inject(callback, doc) {
if (!doc) {
return;
}
while (this.ready === false) {
await new Promise((resolve) => {
const queryTimer = new SafeAnimationFrame(resolve);
queryTimer.start(1);
});
}
doc.documentElement.appendChild(this.frame);
if (userjs.isFN(callback)) {
callback.call({}, this.root);
}
}
remove() {
this.frame.remove();
}
onFrameLoad(iFrame) {
/**
* @type { HTMLIFrameElement }
*/
const target = iFrame.target;
this.root = target.contentDocument.documentElement;
this.ready = true;
dom.cl.add([this.root, target.contentDocument.body], 'mujs-iframe');
// this.root.classList.add('mujs-iframe');
// target.contentDocument.body.classList.add('mujs-iframe');
}
};
const container = new Container();
const primaryFN = (injCon) => {
log('injCon', injCon);
}
/**
* @param { Function } callback
* @returns { null | true }
*/
const loadDOM = (callback) => {
if (!userjs.isFN(callback)) {
return null;
}
if (document.readyState === 'interactive' || document.readyState === 'complete') {
callback.call({}, document);
}
document.addEventListener('DOMContentLoaded', (evt) => callback.call({}, evt.target), {
once: true
});
return true;
};
messenger.onMessage.addListener((root = {}) => {
const m = root.msg;
log('Start', root);
if (root.channel === 'Config' && userjs.isEmpty(cfg)) {
cfg = m.cfg || cfg;
info('Config:', cfg);
loadDOM((doc) => {
try {
if (window.location === null) {
err('"window.location" is null, reload the webpage or use a different one');
return;
}
if (doc === null) {
err('"doc" is null, reload the webpage or use a different one');
return;
}
sleazyRedirect();
container.inject(primaryFN, doc);
} catch (ex) {
err(ex);
}
});
}
});
// class MyCustomElement extends HTMLElement {
// static observedAttributes = ['color', 'size']
// constructor() {
// // Always call super first in constructor
// super()
// }
// connectedCallback() {
// console.log('Custom element added to page.')
// }
// disconnectedCallback() {
// console.log('Custom element removed from page.')
// }
// adoptedCallback() {
// console.log('Custom element moved to new page.')
// }
// attributeChangedCallback(name, oldValue, newValue) {
// log(`Attribute ${name} has changed. ${oldValue} => ${newValue}`)
// }
// }
// customElements.define('mujs-table', MyCustomElement)