-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
46 lines (40 loc) · 1.43 KB
/
main.js
File metadata and controls
46 lines (40 loc) · 1.43 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
const onCodeClick = function(event) {
// This callback based on a snippet from https://css-tricks.com/force-selection-text-block/
const instructions = document.getElementById('instructions');
const codeWrapper = document.getElementById('oauthCode');
// Select the email link anchor text
const range = document.createRange();
range.selectNode(codeWrapper);
window.getSelection().addRange(range);
try {
// Now that we've selected the anchor text, execute the copy command
const successful = document.execCommand('copy');
const msg = successful ? 'successful' : 'unsuccessful';
console.log('Copy command was ' + msg);
if (successful) {
instructions.innerText = 'Code copied succesfully!';
} else {
instructions.innerText += ' Code did not copy, please do it manually.';
}
} catch (err) {
console.log('Oops, unable to copy');
}
};
window.onload = function() {
const codeWrapper = document.getElementById('oauthCode');
const pairs = window.location.search.slice(1).split('&');
let vars = {};
// Grab all the GET variables
pairs.map(currentPair => {
let [key, value] = currentPair.split('=');
vars[key] = value;
});
if (vars['code']) {
// Assuming we the code exists, change the text.
codeWrapper.innerText = vars['code'];
// Add event listener
codeWrapper.addEventListener('click', onCodeClick);
} else {
codeWrapper.innerText = 'No code found.';
}
};