Skip to content

Commit 0f2e803

Browse files
committed
Add copy to clipboard buttons to display token page
1 parent 34b3502 commit 0f2e803

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

pkg/server/tokenrequest/tokenrequest.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ const cssStyle = `
214214
code,pre { font-family: Menlo, Monaco, Consolas, monospace; }
215215
code { font-weight: 300; font-size: 1.5em; margin-bottom: 1em; display: inline-block; color: #646464; }
216216
pre { padding-left: 1em; border-radius: 5px; color: #003d6e; background-color: #EAEDF0; padding: 1.5em 0 1.5em 4.5em; white-space: normal; text-indent: -2em; }
217+
pre>button { margin-left: 1.5em; margin-right: 1.5em; float: right; }
217218
a { color: #00f; text-decoration: none; }
218219
a:hover { text-decoration: underline; }
219220
button { background: none; border: none; color: #00f; text-decoration: none; font: inherit; padding: 0; }
@@ -224,6 +225,26 @@ const cssStyle = `
224225
</style>
225226
`
226227

228+
const jsHandleCopyToClipboard = `
229+
<script>
230+
const isCopyButton = (node) => node.localName === "button"
231+
&& node.attributes.getNamedItem("role") !== null
232+
&& node.attributes.getNamedItem("role").value === "copy-to-clipboard";
233+
234+
document.querySelectorAll("button[role=copy-to-clipboard]").forEach((button) => {
235+
button.onclick = () => {
236+
const textToCopy = Array.from(button.parentElement.childNodes.values()).reduce((acc, node) => {
237+
return isCopyButton(node) ? acc : acc + node.textContent;
238+
}, "");
239+
240+
if (navigator.clipboard) {
241+
navigator.clipboard.writeText(textToCopy);
242+
}
243+
};
244+
});
245+
</script>
246+
`
247+
227248
var tokenTemplate = template.Must(template.New("tokenTemplate").Parse(
228249
cssStyle + `
229250
{{ if .Error }}
@@ -233,10 +254,10 @@ var tokenTemplate = template.Must(template.New("tokenTemplate").Parse(
233254
<code>{{.AccessToken}}</code>
234255
235256
<h2>Log in with this token</h2>
236-
<pre>oc login <span class="nowrap">--token={{.AccessToken}}</span> <span class="nowrap">--server={{.PublicMasterURL}}</span></pre>
257+
<pre>oc login <span class="nowrap">--token={{.AccessToken}}</span> <span class="nowrap">--server={{.PublicMasterURL}}</span><button role="copy-to-clipboard">Copy</button></pre>
237258
238259
<h3>Use this token directly against the API</h3>
239-
<pre>curl <span class="nowrap">-H "Authorization: Bearer {{.AccessToken}}"</span> <span class="nowrap">"{{.PublicMasterURL}}/apis/user.openshift.io/v1/users/~"</span></pre>
260+
<pre>curl <span class="nowrap">-H "Authorization: Bearer {{.AccessToken}}"</span> <span class="nowrap">"{{.PublicMasterURL}}/apis/user.openshift.io/v1/users/~"</span><button role="copy-to-clipboard">Copy</button></pre>
240261
{{ end }}
241262
242263
<br><br>
@@ -251,7 +272,7 @@ var tokenTemplate = template.Must(template.New("tokenTemplate").Parse(
251272
</button>
252273
</form>
253274
{{ end }}
254-
`))
275+
` + jsHandleCopyToClipboard))
255276

256277
var formTemplate = template.Must(template.New("formTemplate").Parse(
257278
cssStyle + `

0 commit comments

Comments
 (0)