Created
May 6, 2018 03:50
-
-
Save zheeeng/621051b593f86dba35745e7a2e8e9df8 to your computer and use it in GitHub Desktop.
addCopyright
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @description 添加版权 | |
*/ | |
const addCopyright = () => { | |
const genCopy = () => { | |
return [ | |
'', | |
'', | |
'作者:Vanessa', | |
'链接:https://hacpai.com/article/1510544423932', | |
'来源:黑客派', | |
'著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。', | |
] | |
} | |
$('.content-reset').on('copy', function (event) { | |
if (!window.getSelection) { | |
return | |
} | |
var selectionObj = window.getSelection() | |
var author = $(this).data('author') || 'HacPai' | |
var link = $(this).data('link') || location.href | |
if (selectionObj.toString().length < 128) { | |
return | |
} | |
if (selectionObj.rangeCount) { | |
var container = document.createElement("div"); | |
for (var i = 0, len = selectionObj.rangeCount; i < len; ++i) { | |
container.appendChild(selectionObj.getRangeAt(i).cloneContents()); | |
} | |
} | |
if ('object' === typeof event.originalEvent.clipboardData) { | |
event.originalEvent.clipboardData.setData('text/html', container.innerHTML + genCopy(author, link).join('<br>')) | |
container.remove(); | |
event.originalEvent.clipboardData.setData('text/plain', selectionObj.toString() + genCopy(author, link).join('\n')) | |
event.preventDefault(); | |
return | |
} | |
$('body').append('<div id="symFixCopy" style="position: fixed; left: -9999px;">' + | |
selectionObj.toString() + genCopy(author, link).join('<br>') + '</div>') | |
window.getSelection().selectAllChildren($('#symFixCopy')[0]) | |
setTimeout(function () { | |
$('#symFixCopy').remove() | |
}, 200) | |
}) | |
} | |
// 作者:Vanessa | |
// 链接:https://hacpai.com/article/1510544423932 | |
// 来源:黑客派 | |
// 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment