forked from auth0/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch-script
83 lines (70 loc) · 2.9 KB
/
fetch-script
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
<style>
/* Style the "Copy Code" button */
.copy-button {
display: inline-block;
padding: 6px 10px;
background-color: #4CAF50;
color: #fff;
border: none;
cursor: pointer;
border-radius: 3px;
margin-top: 10px;
transition: background-color 0.3s;
}
.copy-button:hover {
background-color: #45a049;
}
/* Position the "Copy Code" button */
.copy-button {
position: relative;
top: 5px;
right: 5px;
}
</style>
<div id="code-snippet-container" class="language-javascript"></div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const codeSnippetContainer = document.getElementById('code-snippet-container');
// Check if the element with the ID 'code-snippet-container' exists
if (codeSnippetContainer) {
// Check if the code snippet is already present using a flag
if (!codeSnippetContainer.dataset.codeBlockAdded) {
// Set the flag to prevent duplication
codeSnippetContainer.dataset.codeBlockAdded = 'true';
// Use a Promise to ensure that the Prism.highlightAll() function is only called once
fetch('https://raw.githubusercontent.com/auth0/auth0-spa-js/f677e3df6943e5850027a696740faa498ab55566/jest.config.js')
.then(response => response.text())
.then(data => {
const codeBlock = document.createElement('code');
codeBlock.classList.add('language-javascript');
codeBlock.textContent = data;
const preBlock = document.createElement('pre');
preBlock.classList.add('language-javascript'); // Add language class
preBlock.tabIndex = 0; // Add tabindex
preBlock.appendChild(codeBlock);
codeSnippetContainer.appendChild(preBlock);
// Create a "Copy Code" button
const copyButton = document.createElement('button');
copyButton.classList.add('copy-button'); // Add the custom style
copyButton.innerText = 'Copy Code';
copyButton.addEventListener('click', copyCode);
codeSnippetContainer.appendChild(copyButton);
// Prism.highlightAll() will only be called after the contents of the code snippet have been loaded
Prism.highlightAll();
});
}
} else {
console.error("Element with ID 'code-snippet-container' not found.");
}
function copyCode() {
const code = codeSnippetContainer.querySelector('pre code');
const textArea = document.createElement('textarea');
textArea.value = code.textContent;
document.body.appendChild(textArea);
textArea.select();
document.execCommand('copy');
document.body.removeChild(textArea);
alert('Code copied to clipboard');
}
});
</script>