我正在编写一个Web应用程序,客户端需要在其中可用的数据一经复制就将数据复制到剪贴板。应当在进行REST调用后立即复制数据,而不是通过用户事件进行复制。 没有浏览器插件,这可能吗?我需要哪些权限?
我只发现this文章很有帮助,但这仅适用于浏览器扩展。
这是服务器通过websockets触发的ajax调用,成功后,我要写入剪贴板:
$.ajax({
url: hosturl + '/get-data?id=' + cookieid
}).then(function(data) {
// data is of type string
if (data.type === 'STRING') {
$('#content-container').html(data.stringData);
// not working
navigator.clipboard.writeText(data.stringData);
// also not working
$('#content-container').select();
document.execCommand("copy");
}
答案 0 :(得分:3)
您可以尝试一些黑客手段将数据复制到剪贴板。
方法之一是创建一个临时元素,聚焦并触发操作。
beurs.addAandeel(aandeelTabel[i]);
public class Hoofd
public static void main(String[ ] args)
{
// maak de nodige objecten aan
Beurs beurs = new Beurs();
Portefeuille pf = new Portefeuille("Tom Richmann",4,beurs);
Aandeel[] aandeelTabel = new Aandeel[5];
double[ ] wAgs = {2.0,1.8,1.7,1.6,1.2,1.3,1.2,1.4,1.3,1.6,1.7,1.6};
double[ ] wBekb = {80,71,62,50,48,49,47,37,32,30,28,24};
double[ ] wColr = {40,42,34,35,34,37,30,32,28,30,33,32};
double[ ] wKbc = {28,27,25,23,21,18,14,12,9,13,15,14};
double[ ] wUcb = {32,33,31,30,28,32,33,30,31,34,26,40};
aandeelTabel[0] = new Aandeel("AGS","Ageas",0.2, wAgs);
aandeelTabel[1] = new Aandeel("BEKB","Bekaert",25,wBekb);
aandeelTabel[2] = new Aandeel("COLR","Colruyt",10,wColr);
aandeelTabel[3] = new Aandeel("KBC","KBC",2,wKbc);
aandeelTabel[4] = new Aandeel("UCB","UCB",0.2,wUcb);
// voeg de aandelen toe aan het beurs object.
for(int i=0; i<aandeelTabel.length; i++){
beurs.addAandeel(aandeelTabel[i]);
}
// geef een performance lijst
System.out.println(beurs);
// maak een aantal lijnen aan in de portefeuille
pf.maakNieuweLijn("AGS", 2000, 1.8, "BRU", "23/10/2010");
pf.maakNieuweLijn("COLR", 85, 36, "BRU", "24/10/2010");
pf.maakNieuweLijn("BEKB", 100, 73, "BRU", "2/5/2010");
pf.maakNieuweLijn("AGS", 1000, 1.0, "BRU", "24/11/2010");
// onbekend aandeel
pf.maakNieuweLijn("SMG", 900, 210, "FRA", "23/10/2010");
pf.maakNieuweLijn("UCB", 500, 30, "BRU", "6/6/2010");
// geef een overzicht van de portefeuille
System.out.println(pf.overzichtPortefeuille());
// geef de best presterende lijn !
System.out.println("De best presterende lijn :" +
pf.bestperformingLijn());
}
有关其他示例,您可以参考this article
此外,还有一个新的API-function copyToClipboard(str) {
var el = document.createElement('textarea'); // Create a <textarea> element
el.value = str; // Set its value to the string that you want copied
el.setAttribute('readonly', ''); // Make it readonly to be tamper-proof
el.style.position = 'absolute';
el.style.left = '-9999px'; // Move outside the screen to make it invisible
document.body.appendChild(el); // Append the <textarea> element to the HTML document
var selected =
document.getSelection().rangeCount > 0 // Check if there is any content selected previously
? document.getSelection().getRangeAt(0) // Store selection if found
: false; // Mark as false to know no selection existed before
el.select(); // Select the <textarea> content
document.execCommand('copy'); // Copy - only works as a result of a user action (e.g. click events)
document.body.removeChild(el); // Remove the <textarea> element
if (selected) { // If a selection existed before copying
document.getSelection().removeAllRanges(); // Unselect everything on the HTML document
document.getSelection().addRange(selected); // Restore the original selection
}
};
,但现在仅适用于少数浏览器-Clipboard API support