TCGPlayer

Adds wishlist functionality to TCGPlayer.com. Saves locally to browser's localstorage.

当前为 2018-02-16 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name TCGPlayer
  3. // @namespace http://MNSweet.com/
  4. // @version 1.0
  5. // @description Adds wishlist functionality to TCGPlayer.com. Saves locally to browser's localstorage.
  6. // @author MNSweet
  7. // @match https://shop.tcgplayer.com/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. function addToWishlist(name,url) {
  13. }
  14.  
  15. (function() {
  16. 'use strict';
  17. jQuery(document).ready(function($) {
  18. if (typeof(Storage) !== "undefined") {
  19. var item = $('.product-details__name').text().trim();
  20. if(localStorage.getItem("wishlist") === null) {localStorage.setItem("wishlist",'{}');}
  21. if(item in JSON.parse(localStorage.getItem("wishlist"))) {
  22. $('.page--product-details .product-details__image ')
  23. .append('<a href="#" class="showWishlist btn btn--add-to-cart" title="'+$('.product-details__name').text().trim()+'" rel="'+encodeURIComponent($('#cardImage').attr('src'))+'" ref="'+encodeURIComponent(window.location.href)+'">Open Wishlist</a>');
  24.  
  25. }else {
  26. $('.page--product-details .product-details__image ')
  27. .append('<a href="#" class="addToWishlist btn btn--add-to-cart" title="'+$('.product-details__name').text().trim()+'" rel="'+encodeURIComponent($('#cardImage').attr('src'))+'" ref="'+encodeURIComponent(window.location.href)+'">Add to Wishlist</a>');
  28. }
  29. $('.dropdown-menu--acount .dropdown-item:nth-child(3)').after('<a class="dropdown-item" id="showWishlist" class="showWishlist" href="#">Wishlist</a>');
  30. }
  31. $(document).on("click", ".addToWishlist", function() {
  32. event.preventDefault();
  33. var wish = $(this);
  34. if(wish.text() == 'ADDED') {return;}
  35. var wishlist = JSON.parse(localStorage.getItem("wishlist"));
  36. wishlist[wish.attr('title')] = {
  37. 'qty':1,
  38. 'notes':'',
  39. 'url':wish.attr('ref'),
  40. 'img':wish.attr('rel')
  41. };
  42. localStorage.setItem("wishlist", JSON.stringify(wishlist));
  43. wish.css({
  44. 'color':'#fff'
  45. });
  46. wish.removeClass('addToWishlist').addClass('showWishlist').text('Open Wishlist');
  47. showWishlist();
  48. });
  49. $(document).on("click", ".showWishlist", showWishlist);
  50. function showWishlist() {
  51. event.preventDefault();
  52. var wishlist = JSON.parse(localStorage.getItem("wishlist"));
  53. var items = renderWishlist(wishlist);
  54. $('body').append('<div id="wishlistModal"><h2>Wishlist</h2>'+items+'<a href="#" id="wishlistClose" style="position: fixed;top: 5vh;left: calc( 95vw - 41px );border-right: 41px solid #000;background: #000;color:#fff;width: 25px;text-indent: 9px;border-bottom-left-radius: 20px;height: 25px;">X</a>'+'</div>');
  55. $('#wishlistModal').css({
  56. 'width': '90vw',
  57. 'height': '90vh',
  58. 'position': 'fixed',
  59. 'text-align':'center',
  60. 'top': '5vh',
  61. 'left': '5vw',
  62. 'border': '1px solid #999999',
  63. 'z-index': '1000',
  64. 'overflow':'auto',
  65. 'background': 'rgba(255,255,255,0.97)'
  66. });
  67. $('.wishlistRemove').css({
  68. 'position': 'absolute',
  69. 'top': '4px',
  70. 'right': '4px',
  71. 'background':'#fff',
  72. 'width': '30px',
  73. 'text-indent': '10px',
  74. 'line-height': '31px',
  75. 'border-radius': '20px 0 0 20px',
  76. 'height':'30px',
  77. 'color': '#000)'
  78. });
  79. $('.wishlistRemove:after').css({
  80. 'content': '',
  81. 'display': 'table',
  82. 'clear': 'both'
  83. });
  84. }
  85. function renderWishlist(wishlist){
  86. var items = '';
  87. for(var item in wishlist) {
  88. items += '<div class="wishlist-item" style="display:inline-block;text-align:left;width:33.3%;padding:5px;box-sizing:border-box;position:relative;border: 1px solid #ddd;border-style: none solid solid none;">'+
  89. '<h6 style="background: #000;padding: 6px;"><a class="wishlist-item-header" style="color:#fff;" href="'+decodeURIComponent(wishlist[item].url)+'">'+item+'</a></h6>'+
  90. '<a class="wishlist-item-img-link" style="display:block;width:150px;height:234px;overflow:hidden;float:left;margin-right:10px;" href="'+decodeURIComponent(wishlist[item].url)+'">'+
  91. '<img style="display:block;" width="150" src="'+decodeURIComponent(wishlist[item].img)+'"/>'+
  92. '</a>'+
  93. '<strong>Desired Quantity:</strong> <input rel="'+item+'" class="wishlist-item-qty" type="number" style="width:3em;background:transparent;border:0;" value="'+wishlist[item].qty+'"/><br/>'+
  94. '<strong>Notes:</strong><br/><textarea rel="'+item+'" class="wishlist-item-notes" style="resize: none;width: calc(100% - 160px);height: calc(234px - 75px);border:1px solid #f1f1f1;">'+wishlist[item].notes+'</textarea>'+
  95. '<a href="#" class="wishlistRemove" title="'+item+'">X</a>'+
  96. '</div>';
  97. }
  98. return items;
  99. }
  100. $(document).on("click", ".wishlistRemove", function() {
  101. event.preventDefault();
  102. var wish = $(this);
  103. var wishlist = JSON.parse(localStorage.getItem("wishlist"));
  104. delete wishlist[wish.attr('title')];
  105. var items = renderWishlist(wishlist);
  106. $('#wishlistModal').html('<h2>Wishlist</h2>'+items+'<a href="#" id="wishlistClose" style="position: fixed;top:5vh;left:calc( 95vw - 41px );border-right: 41px solid #000;background: #000;color:#fff;width: 25px;text-indent: 9px;border-bottom-left-radius: 20px;height: 25px;">X</a>');
  107. localStorage.setItem("wishlist", JSON.stringify(wishlist));
  108.  
  109. $('.wishlistRemove').css({
  110. 'position': 'absolute',
  111. 'top': '4px',
  112. 'right': '4px',
  113. 'background':'#fff',
  114. 'width': '30px',
  115. 'text-indent': '10px',
  116. 'line-height': '31px',
  117. 'border-radius': '20px 0 0 20px',
  118. 'height':'30px',
  119. 'color': '#000'
  120. });
  121. $('.wishlistRemove:after').css({
  122. 'content': '',
  123. 'display': 'table',
  124. 'clear': 'both'
  125. });
  126. if($('.product-details__name').text().trim() == wish.attr('title') ) {
  127. $('.showWishlist.btn--add-to-cart').removeClass('showWishlist').addClass('addToWishlist').text('Add to Wishlist');
  128. }
  129. });
  130. });
  131. $(document).on("change", ".wishlist-item-qty", function() {
  132. var wish = $(this);
  133. var wishlist = JSON.parse(localStorage.getItem("wishlist"));
  134. wishlist[wish.attr('rel')].qty = wish.val();
  135. localStorage.setItem("wishlist", JSON.stringify(wishlist));
  136. });
  137. $(document).on("keyup", ".wishlist-item-notes", function() {
  138. var wish = $(this);
  139. var wishlist = JSON.parse(localStorage.getItem("wishlist"));
  140. wishlist[wish.attr('rel')].notes = wish.val();
  141. localStorage.setItem("wishlist", JSON.stringify(wishlist));
  142. });
  143. $(document).on("click", "#wishlistClose", function() {
  144. $('#wishlistModal').remove();
  145. });
  146.  
  147. })();