[英]How do I copy to clipboard in Safari 8 on Yosemite OS X?
我正在運行confluence並在 Yosemite 操作系統上使用 Safari 8 訪問它。 Confluence 有一個很好的功能,稱為“ 共享頁面”,它會生成一個TinyURL並允許用戶單擊下面顯示的“復制”按鈕,將微小的 URL 復制到您的剪貼板。
單擊“復制”然后嘗試粘貼(使用 CRTL-V)在 Safari v8 中不起作用,但在 Yosemite 上的 Firefox Quantum v60 中起作用。 但是,先執行 CRTL-C 再執行 CRTL-V 對 Safari v8 和 Firefox 60 都有效。
如何讓“復制”按鈕在 Safari v8 上工作?
我挖得更深一點,發現 javascript 文件執行下面的“復制到剪貼板”功能。 在“在 Firefox 60 中工作,而不是 Safari 8”下面的最后查找我的評論。 該行應該將 Tiny URL 復制到剪貼板。 它在 Firefox 中成功,但在 Safari 中沒有成功。我如何更改代碼以使其在 Safari 8 中工作?
define('confluence/share-page/popup/setup-share-link', [
'jquery',
'confluence/share-page/service/analytics-service',
'confluence/share-page/util/show-message'
], function ($,
analyticsService,
showMessage) {
/**
* Resolves the link. If it's a function, it will evaluate it, otherwise
* it will just return it if it's a string, or return window.location if it's not
* either of those things.
* @param {function|string} link
* @private
*/
function _resolveLink(link) {
if (typeof link === 'function') {
return $.when(link());
}
if (typeof link === 'string') {
return $.when(link);
}
return $.when(window.location);
}
/**
* Adds analytics parameters to link. Returns the new link with query
* parameters concatenated.
* @param {string} link
* @private
*/
function _addAnalyticsToLink(link) {
if (("" + link).indexOf('resumedraft.action') === -1) {
return link;
}
var analyticsQueryParamString = 'src=shareui' +
'&src.shareui.timestamp=' + (new Date()).getTime();
var firstChar;
if (window.location.search.length === 0) {
firstChar = '?';
} else {
firstChar = '&';
}
return link + firstChar + analyticsQueryParamString;
}
/**
* Sets up the Share Link and button to copy the link to clipboard
* @param $container
* @param parameters The dialog's parameters passed-through
*/
return function setupShareLink($container, parameters) {
var $row = $container.find('.share-copy-link');
var $shareLink = $row.find('input');
var $copyButton = $row.find('button');
// Don't set it up if it doesn't exist
if (!$shareLink.length) {
return;
}
// Set the default link, then get the real link and update it;
$shareLink.val(window.location);
_resolveLink(parameters.link).then(function (link) {
link = _addAnalyticsToLink(link);
$shareLink.val(link);
});
$shareLink.on('click focus',
/**
* Select all the contents of the input field on click
*/
function (e) {
setTimeout(function () {
_selectLink();
}, 0);
e.preventDefault();
});
$shareLink.focus(function () {
analyticsService.publish(analyticsService.SHARE_LINK_CLICKED, parameters.entityId, parameters.shareType);
});
$copyButton.click(function () {
analyticsService.publish(analyticsService.SHARE_LINK_COPY_CLICKED, parameters.entityId, parameters.shareType);
_copy();
});
$shareLink.dblclick(function () {
analyticsService.publish(analyticsService.SHARE_LINK_DOUBLE_CLICKED, parameters.entityId, parameters.shareType);
_copy();
});
$shareLink.blur(function () {
$shareLink.removeData('selected');
});
$(document).off('copy.share-link').on('copy.share-link', function () {
if (!$shareLink.data('selected')) {
return;
}
analyticsService.publish(analyticsService.SHARE_LINK_COPIED, parameters.entityId, parameters.shareType);
showMessage($row, 'copied', parameters, false, function () {
$copyButton.prop('disabled', true);
}, function () {
$copyButton.prop('disabled', false);
});
});
/**
* Select all the contents of the input field when the mouse
* is there
*/
function _selectLink() {
$shareLink.select();
$shareLink.data('selected', true);
}
/**
* Copies the link to the clipboard
*/
function _copy() {
_selectLink();
document.execCommand('copy'); // *** works in Firefox 60, not Safari 8
}
}
});
這是原始copyText
中的 copyText。
var doc, nav, I, copyText; // for use on other loads addEventListener('load', function(){ doc = document; nav = navigator; I = function(id){ return doc.getElementById(id); } if(nav.clipboard){ copyText = function(fromNode){ fromNode.select(); nav.clipboard.writeText(fromNode.value.trim()); } } else{ copyText = function(fromNode){ fromNode.select(); doc.execCommand('copy'); } } // magic under here - except end load var test = I('test'), test_button = I('test_button'); test_button.onclick = function(){ copyText(test); } }); // end load
<input id='test' type='text' value='' /><input id='test_button' type='button' value='copy' />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.