通用function

try to take over the world!

目前为 2020-01-13 提交的版本,查看 最新版本

此脚本不应直接安装,它是供其他脚本使用的外部库。如果你需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/395074/765186/%E9%80%9A%E7%94%A8function.js

  1. //copy文本内容
  2. function copy(value) {
  3. let transfer = document.createElement('input');
  4. document.body.appendChild(transfer);
  5. transfer.value = value; // 这里表示想要复制的内容
  6. //transfer.focus();
  7. transfer.select();
  8. if (document.execCommand('copy')) {
  9. document.execCommand('copy');
  10. }
  11. //transfer.blur();
  12. console.log('复制成功:',value);
  13. document.body.removeChild(transfer);
  14. }
  15.  
  16. //copy图片到剪切板
  17. function copyImage(img=''){
  18. img = img || document.getElementsByTagName('img')[0];
  19. if(img == undefined){
  20. console.log('img not exits');
  21. return ;
  22. }
  23. img.removeAttribute('alt');
  24. var range = document.createRange();
  25. range.selectNode(img);
  26. window.getSelection().removeAllRanges();
  27. window.getSelection().addRange(range);
  28. document.execCommand("Copy");
  29. console.log('复制成功:',img.src);
  30. }