WykopSeen

Add Seen button to hide seen wykops

当前为 2015-01-05 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name WykopSeen
  3. // @namespace http://www.wykop.pl/
  4. // @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
  5. // @version 0.9
  6. // @description Add Seen button to hide seen wykops
  7. // @author axem.pl
  8. // @match http://www.wykop.pl/
  9. // @match http://www.wykop.pl/link/*
  10. // @match http://www.wykop.pl/wykopalisko/*
  11. // @grant GM_getValue
  12. // @grant GM_setValue
  13. // @grant GM_deleteValue
  14. // ==/UserScript==
  15.  
  16. var jq = this.$ = this.jQuery = jQuery.noConflict(true);
  17. var seenWykopClass = 'seen';
  18.  
  19. function eachLi() {
  20. var wykopitem = jq(this);
  21. var wykopitemparent = wykopitem.parent();
  22. var btn = jq('<button class=\'seen-btn\'></button>');
  23. var href = wykopitem.find('h2 a').attr('href');
  24. if (!href) return;
  25. var isseen = getIsSeen(href);
  26.  
  27. function btnOnClick2(event) {
  28. var result = btnOnClick.call(this, event);
  29. var jqThis = jq(this);
  30. var isseen = jqThis.data('isseen');
  31.  
  32. if (isseen) {
  33. wykopitem.addClass(seenWykopClass);
  34. wykopitemparent.append(wykopitem);
  35. } else {
  36. wykopitem.removeClass(seenWykopClass);
  37. wykopitemparent.prepend(wykopitem);
  38. }
  39.  
  40. return false;
  41. }
  42. if (isseen) {
  43. wykopitem.addClass(seenWykopClass);
  44. wykopitemparent.append(wykopitem);
  45. }
  46. btn.data('href', href).data('isseen', isseen).on('click', btnOnClick2);
  47. refreshBtnText.call(btn);
  48.  
  49. wykopitem.children('div').append(btn);
  50. }
  51. function refreshBtnText() {
  52. var jqThis = jq(this);
  53.  
  54. if (jqThis.data('isseen')) {
  55. jqThis.text('UNSEE');
  56. } else {
  57. jqThis.text('SEEN');
  58. }
  59. }
  60. function btnOnClick(event) {
  61. if (event) event.preventDefault();
  62. var jqThis = jq(this);
  63. var href = jqThis.data('href');
  64. var isseen = jqThis.data('isseen');
  65. isseen = !isseen;
  66. jqThis.data('isseen', isseen);
  67. setIsSeen(href, isseen);
  68. refreshBtnText.call(jqThis);
  69. return false;
  70. }
  71. function getIsSeen(href) {
  72. return GM_getValue(href);
  73. }
  74. function setIsSeen(href, value) {
  75. if (value) {
  76. GM_setValue(href, 1);
  77. } else {
  78. GM_deleteValue(href);
  79. }
  80. }
  81. function seenFullBtn() {
  82. var btn = jq('<button class=\'seen-btn\'></button>');
  83. var href = getHrefFromLocation();
  84. if (!href) return;
  85. var isseen = getIsSeen(href);
  86.  
  87. btn.data('href', href).data('isseen', isseen).on('click', btnOnClick);
  88. refreshBtnText.call(btn);
  89. jq('.article.fullview .lcontrast').append(btn);
  90. }
  91. function cssRules() {
  92. var rules = [
  93. 'li.seen { background: lightgray }',
  94. 'li.seen * { color: gray !important }',
  95. 'li.seen .diggbox { display: none }',
  96. 'li.seen .media-content { display: none }',
  97. 'li.seen .fix-tagline { display: none }',
  98. 'li.seen .description { display: none }',
  99. 'li.seen .elements { display: none }',
  100. 'li.seen .article { min-height: 0 }',
  101. '.seen-btn { position: absolute; top: 5px; right: 5px; z-index: 9999 }'
  102. ];
  103. return rules.join('\n');
  104. }
  105. function getHrefFromLocation() {
  106. var href = document.location.toString().split('#')[0];
  107. if (href.lastIndexOf('http://www.wykop.pl/link/', 0) === 0) {
  108. return href;
  109. }
  110. }
  111. function makeSeenLink() {
  112. var href = getHrefFromLocation();
  113. if (href) {
  114. setIsSeen(href, true);
  115. }
  116. }
  117. function exec() {
  118. makeSeenLink();
  119. jq(document.body).append(jq('<style type=\'text/css\'></style>').html(cssRules()));
  120. jq('#itemsStream li').each(eachLi);
  121. seenFullBtn();
  122. }
  123. exec();