通用function

try to take over the world!

目前为 2020-01-15 提交的版本。查看 最新版本

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

  1. // ==UserScript==
  2. // @name 通用function
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description try to take over the world!
  6. // @author banxia
  7. // @match */*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. //copy文本内容
  15. function copy(value) {
  16. let transfer = document.createElement('input');
  17. document.body.appendChild(transfer);
  18. transfer.value = value; // 这里表示想要复制的内容
  19. //transfer.focus();
  20. transfer.select();
  21. if (document.execCommand('copy')) {
  22. document.execCommand('copy');
  23. }
  24. //transfer.blur();
  25. console.log('复制成功:',value);
  26. document.body.removeChild(transfer);
  27. }
  28.  
  29. //copy图片到剪切板
  30. function copyImage(img=''){
  31. img = img || document.getElementsByTagName('img')[0];
  32. if(img == undefined){
  33. console.log('img not exits');
  34. return ;
  35. }
  36. img.removeAttribute('alt');
  37. var range = document.createRange();
  38. range.selectNode(img);
  39. window.getSelection().removeAllRanges();
  40. window.getSelection().addRange(range);
  41. document.execCommand("Copy");
  42. window.getSelection().removeAllRanges();
  43. console.log('复制成功:',img.src);
  44. }
  45.  
  46. // Your code here...
  47. })();