-
Notifications
You must be signed in to change notification settings - Fork 0
/
waitall.js
193 lines (167 loc) · 6.51 KB
/
waitall.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
function waitForElm(selector) {
return new Promise(resolve => {
if (document.querySelector(selector)) {
return resolve(document.querySelector(selector));
} else {
return resolve(null);
}
const observer = new MutationObserver(mutations => {
if (document.querySelector(selector)) {
resolve(document.querySelector(selector));
observer.disconnect();
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
});
}
function setNativeValue(element, value) {
let lastValue = element.value;
element.value = value;
let event = new Event("input", { target: element, bubbles: true });
// React 15
event.simulated = true;
// React 16
let tracker = element._valueTracker;
if (tracker) {
tracker.setValue(lastValue);
}
element.dispatchEvent(event);
}
async function waitWithVal(selector, value) {
const el = await waitForElm(selector);
if (el) {
setNativeValue(el, value);
}
}
async function waitAndClick(selector) {
const el = await waitForElm(selector);
if (el) {el.click();}
}
async function waitAndFocus(selector) {
const el = await waitForElm(selector);
if (el) {el.focus();}
}
async function waitFocusWithVal(selector, value) {
const el = await waitForElm(selector);
if (el) {
el.focus();
el.value = value
}
}
async function waitClickWithVal(selector, value) {
const el = await waitForElm(selector);
if (el) {
el.click();
el.value = value
}
}
async function valAll(selectors, value) {
for (let i = 0; i < selectors.length; i++) {
await waitWithVal(selectors[i], value);
}
}
async function clickAll(selectors) {
for (let i = 0; i < selectors.length; i++) {
await waitAndClick(selectors[i]);
}
}
function copyBoard(props) {
const board = document.querySelector('#mmcopyBoard');
if (!board) {
const wrapDiv = document.createElement('div');
wrapDiv.id = 'wrappermmcopyBoard';
wrapDiv.style = 'position:fixed;background:blue;width:90%;left:50px;top:50px;z-index:9999;';
const boardDiv = document.createElement('div');
boardDiv.id = 'mmcopyBoard';
boardDiv.style = 'position:absolute;background:green;user-select: none;padding:10px;width:100%;';
boardDiv.onmousedown = function(){mydragg.startMoving(this,"wrappermmcopyBoard",event);}
boardDiv.onmouseup = function(){mydragg.stopMoving("wrappermmcopyBoard");}
boardDiv.ondblclick = function(){boardDiv.style.display = 'none';};
wrapDiv.appendChild(boardDiv);
Object.keys(props).forEach((prop) => {
const button = document.createElement('button');
button.innerHTML = prop;
button.style = 'cursor:pointer;margin:5px !important;color:blue !important;padding 2px !important;border:2px solid green !important;min-width:auto !important;min-height: auto !important;scale:1 !important;width:auto !important;height:auto !important;';
button.onclick = function(){
copyTextToClipboard(props[prop]);
};
boardDiv.appendChild(button);
});
document.body.appendChild(wrapDiv);
}
}
function copyTextToClipboard(text) {
var textArea = document.createElement("textarea");
// Place in the top-left corner of screen regardless of scroll position.
textArea.style.position = 'fixed';
textArea.style.top = 0;
textArea.style.left = 0;
// Ensure it has a small width and height. Setting to 1px / 1em
// doesn't work as this gives a negative w/h on some browsers.
textArea.style.width = '2em';
textArea.style.height = '2em';
// We don't need padding, reducing the size if it does flash render.
textArea.style.padding = 0;
// Clean up any borders.
textArea.style.border = 'none';
textArea.style.outline = 'none';
textArea.style.boxShadow = 'none';
// Avoid flash of the white box if rendered for any reason.
textArea.style.background = 'transparent';
textArea.value = text;
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
//console.log('Copying text command was ' + msg);
} catch (err) {
//console.log('Oops, unable to copy');
}
document.body.removeChild(textArea);
}
var mydragg = function() {
return {
move: function(divid, xpos, ypos) {
divid.style.left = xpos + 'px';
divid.style.top = ypos + 'px';
},
startMoving: function(divid, container, evt) {
evt = evt || window.event;
var posX = evt.clientX,
posY = evt.clientY,
divTop = divid.style.top,
divLeft = divid.style.left,
eWi = parseInt(divid.style.width),
eHe = parseInt(divid.style.height),
cWi = parseInt(document.getElementById(container).style.width),
cHe = parseInt(document.getElementById(container).style.height);
document.getElementById(container).style.cursor = 'move';
divTop = divTop.replace('px', '');
divLeft = divLeft.replace('px', '');
var diffX = posX - divLeft,
diffY = posY - divTop;
document.onmousemove = function(evt) {
evt = evt || window.event;
var posX = evt.clientX,
posY = evt.clientY,
aX = posX - diffX,
aY = posY - diffY;
if (aX < 0) aX = 0;
if (aY < 0) aY = 0;
if (aX + eWi > cWi) aX = cWi - eWi;
if (aY + eHe > cHe) aY = cHe - eHe;
mydragg.move(divid, aX, aY);
}
},
stopMoving: function(container) {
var a = document.createElement('script');
document.getElementById(container).style.cursor = 'default';
document.onmousemove = function() {}
},
}
}();