TVMaze: add "Watched" button to calendar

Add add "Watched" button to calendar at TVMaze

目前为 2017-01-28 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name TVMaze: add "Watched" button to calendar
  3. // @description Add add "Watched" button to calendar at TVMaze
  4. // @namespace BlackNullerNS
  5. // @include http://www.tvmaze.com/calendar*
  6. // @version 1.0
  7. // @grant GM_xmlhttpRequest
  8. // ==/UserScript==
  9.  
  10. var btn = document.createElement('button');
  11. btn.innerHTML = '✓';
  12. btn.style.border = 0;
  13. btn.style.padding = '2px';
  14. btn.style.background = 'transparent';
  15. btn.style.color = '#3C948B';
  16. btn.style.cursor = 'pointer';
  17.  
  18. var handler = function (e) {
  19. e.preventDefault();
  20.  
  21. if (!confirm('Mark the episode as watched?')) {
  22. return false;
  23. }
  24.  
  25. var self = this,
  26. entry = this.parentNode,
  27. link = this.previousElementSibling;
  28.  
  29. if (link.getAttribute('href').indexOf('episodes/') === -1) {
  30. return false;
  31. }
  32.  
  33. var eid = link.getAttribute('href').split('episodes/')[1].split('/')[0];
  34. var url = '/watch/set?episode_id=' + eid;
  35. var csrf = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
  36.  
  37. GM_xmlhttpRequest({
  38. method: "POST",
  39. url: url,
  40. data: "type=0",
  41. headers: {
  42. "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
  43. "X-CSRF-Token": csrf
  44. },
  45. onload: function(response) {
  46. entry.classList.add('watched');
  47. self.remove();
  48. }
  49. });
  50. };
  51.  
  52. var cloned;
  53. var episodes = document.querySelectorAll(".entry:not(.watched)");
  54.  
  55. for (var i = 0, l = episodes.length; i < l; i++) {
  56. cloned = btn.cloneNode(true);
  57. cloned.addEventListener('click', handler);
  58. episodes.item(i).appendChild(cloned);
  59. }