#160 apply the workaround for non secured context. Not tested

This commit is contained in:
Matthieu Baumann
2025-02-14 09:12:07 +01:00
parent 7994ecc40e
commit ac880608af
2 changed files with 35 additions and 1 deletions

View File

@@ -575,6 +575,38 @@ Utils.download = function(url, name = undefined) {
a.click()
}
// Copy some text to the clipboard
// This provides an alternative when the window context is not secure (e.g. http access)
// Code copied from: https://stackoverflow.com/questions/51805395/navigator-clipboard-is-undefined
Utils.copy2Clipboard = async function(textToCopy) {
// Navigator clipboard api needs a secure context (https)
if (navigator.clipboard && window.isSecureContext) {
await navigator.clipboard.writeText(textToCopy);
} else {
// Use the 'out of viewport hidden text area' trick
const textArea = document.createElement("textarea");
textArea.value = textToCopy;
// Move textarea out of the viewport so it's not visible
textArea.style.position = "absolute";
textArea.style.opacity = "0";
document.body.prepend(textArea);
textArea.select();
try {
// execCommand is deprecated but there seems to be no other
// alternative and is still used by a lot of applications
// See: https://stackoverflow.com/questions/60581285/execcommand-is-now-obsolete-whats-the-alternative
document.execCommand('copy');
} catch (error) {
console.error(error);
} finally {
textArea.remove();
}
}
}
Utils.measureTime = function(msg, method) {
let startTime = performance.now()
let output = method()

View File

@@ -39,6 +39,7 @@ import { ALEvent } from "../events/ALEvent.js";
import { Layout } from "./Layout.js";
import { ActionButton } from "./Widgets/ActionButton.js";
import { Input } from "./Widgets/Input.js";
import { Utils } from "../Utils.ts";
export class Location extends DOMElement {
// constructor
@@ -243,7 +244,8 @@ export class Location extends DOMElement {
copyCoordinatesToClipboard() {
let msg;
navigator.clipboard.writeText(this.field.get())
const cooText = this.field.get();
Utils.copy2Clipboard(cooText)
.then(() => {
msg = 'successful'
if (this.aladin.statusBar) {