我正在尝试以编程方式使用Chrome(Build 43)中的execCommand将异步JSONP请求的结果复制到剪贴板.这是一个逻辑片段:
loadContent()
function loadContent(callback) {
$.getJSON('http://www.randomtext.me/api/lorem/p-5/10-20?&callback=myFunc',function(result){
console.log('result=',result.text_out);
$("#container").html(result.text_out);
if (callback) {
callback();
}
});
}
function copyAjax() {
loadContent(copy);
}
function copy() {
var copyDivText = $('#container').text();
console.log('copyDivText=',copyDivText);
executeCopy(copyDivText);
}
document.addEventListener("DOMContentLoaded", function(){
document.getElementById("copy").onclick = copy;
});
document.addEventListener("DOMContentLoaded", function(){
document.getElementById("copyAjax").onclick = copyAjax;
});
// Copy text as text
function executeCopy(text) {
var input = document.createElement('textarea');
document.body.appendChild(input);
input.value = text;
input.focus();
input.select();
document.execCommand('Copy');
input.remove();
}
Run Code Online (Sandbox Code Playgroud)
我知道你开始编译的43代Chrome使用execCommand和剪贴板.但问题是,您需要在执行用户发起的事件(其中权限被提升)中执行此操作.这是ZeroClipboard基于闪存的解决方案所具有的类似限制.除了得到一个不可能的答案(这是我现在正在思考的问题)之外,这些是我想做的其他选择(警告,它们都是Hail Mary Passes):
我已经看过成问题,#2,如 …
我不能直接复制生成的链接(没有ctrl + C)我是usign document.execCommand('copy')但它似乎没有效果.
如果代码没有AJAX那么它的工作就完美了.这是
HTML:
<div class="permalink-control"> </div>
Run Code Online (Sandbox Code Playgroud)
JQUERY:
$(".permalink-control")
.append(
'<div class="input-group">' +
' <span class="input-group-btn"><button type="button" class="btn btn-default" title="Get Permalink"><span class="glyphicon glyphicon-link"></span></button></span>' +
' <input type="text" class="form-control">' +
'</div>'
);
$(".permalink-control input")
.hide()
.focus(function () {
// Workaround for broken selection: https://stackoverflow.com/questions/5797539
var $this = $(this);
$this.select()
.mouseup(function () {
$this.unbind("mouseup");
return false;
});
});
$(".permalink-control button")
.click(function () {
var $this = $(this);
$.ajax({
url: "https://api-ssl.bitly.com/shorten",
dataType: "jsonp",
data: { …Run Code Online (Sandbox Code Playgroud)