WykopSeen

Add Seen button to hide seen wykops

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

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