eBay copy short url button

Adds a copy short url button to ebay item page. Copies a nice link instead of a long one. The short url - http://www.ebay.com/itm/item_id

目前为 2016-03-03 提交的版本。查看 最新版本

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