eBay copy short url button

adds a copy short url button to ebay item page

目前為 2016-02-29 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name eBay copy short url button
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description adds a copy short url button to ebay item page
  6. // @author Ariel Jannai
  7. // @include /ebay\.(com|de|co\.uk)\/itm/
  8. // @run-at document-end
  9. // @grant none
  10. // ==/UserScript==
  11. /* jshint -W097 */
  12. 'use strict';
  13.  
  14. // Your code here...
  15. var copyLinkIcon = 'http://icons.iconarchive.com/icons/icons8/windows-8/32/Editing-Copy-Link-icon.png';
  16. var copyButton = '<td><div class="copy-url"><a rel="nofollow" href="javascript:;" ' +
  17. 'class="scIcon copy-short-url" aria-label="Copy short url to clipboard" ' +
  18. 'title="Copy short url to clipboard" target="_blank"></a></div></td>';
  19.  
  20. addGlobalStyle('.copy-short-url { background-image: url("' + copyLinkIcon + '"); width: 18px; height: 18px; float: left; background-size: contain;}');
  21. addGlobalStyle('.copy-url { margin-right: 3px; }');
  22.  
  23. $('div.share table tbody tr').prepend(copyButton);
  24.  
  25.  
  26. $('.copy-url')[0].addEventListener('click', function(event) {
  27. copyTextToClipboard(tryShortUrl(document.location.href));
  28. });
  29.  
  30. function tryShortUrl(url) {
  31. var urlPattern = /ebay\.(com|de|co\.uk)\/itm/;
  32.  
  33. if(urlPattern.test(url)) {
  34. var id = url.match(/\/(\d{12})(\?|$)/)[1];
  35. return 'http://www.' + urlPattern.exec(url)[0] + '/' + id;
  36. } else {
  37. return url;
  38. }
  39. }
  40.  
  41. // https://somethingididnotknow.wordpress.com/2013/07/01/change-page-styles-with-greasemonkeytampermonkey/
  42. function addGlobalStyle(css) {
  43. var head, style;
  44. head = document.getElementsByTagName('head')[0];
  45. if (!head) { return; }
  46. style = document.createElement('style');
  47. style.type = 'text/css';
  48. style.innerHTML = css;
  49. head.appendChild(style);
  50. }
  51.  
  52. // http://stackoverflow.com/a/30810322/2350423
  53. function copyTextToClipboard(text) {
  54. var textArea = document.createElement("textarea");
  55.  
  56. //
  57. // *** This styling is an extra step which is likely not required. ***
  58. //
  59. // Why is it here? To ensure:
  60. // 1. the element is able to have focus and selection.
  61. // 2. if element was to flash render it has minimal visual impact.
  62. // 3. less flakyness with selection and copying which **might** occur if
  63. // the textarea element is not visible.
  64. //
  65. // The likelihood is the element won't even render, not even a flash,
  66. // so some of these are just precautions. However in IE the element
  67. // is visible whilst the popup box asking the user for permission for
  68. // the web page to copy to the clipboard.
  69. //
  70.  
  71. // Place in top-left corner of screen regardless of scroll position.
  72. textArea.style.position = 'fixed';
  73. textArea.style.top = 0;
  74. textArea.style.left = 0;
  75.  
  76. // Ensure it has a small width and height. Setting to 1px / 1em
  77. // doesn't work as this gives a negative w/h on some browsers.
  78. textArea.style.width = '2em';
  79. textArea.style.height = '2em';
  80.  
  81. // We don't need padding, reducing the size if it does flash render.
  82. textArea.style.padding = 0;
  83.  
  84. // Clean up any borders.
  85. textArea.style.border = 'none';
  86. textArea.style.outline = 'none';
  87. textArea.style.boxShadow = 'none';
  88.  
  89. // Avoid flash of white box if rendered for any reason.
  90. textArea.style.background = 'transparent';
  91.  
  92.  
  93. textArea.value = text;
  94.  
  95. document.body.appendChild(textArea);
  96.  
  97. textArea.select();
  98.  
  99. try {
  100. var successful = document.execCommand('copy');
  101. var msg = successful ? 'successful' : 'unsuccessful';
  102. console.log('Copying text command was ' + msg + ': ' + text);
  103. } catch (err) {
  104. console.log('Oops, unable to copy');
  105. }
  106.  
  107. document.body.removeChild(textArea);
  108. }