GK parent comment

Adds links to parent commentary to GK comments, and sets parent commentary text as link tooltip.

  1. // ==UserScript==
  2. // @name GK parent comment
  3. // @namespace GK
  4. // @description Adds links to parent commentary to GK comments, and sets parent commentary text as link tooltip.
  5. // @include http://govnokod.ru/*
  6. // @include http://www.govnokod.ru/*
  7. // @version 1.2.0
  8. // @grant unsafeWindow
  9. // ==/UserScript==
  10.  
  11. (function(){
  12. function RunInPage(func) {
  13. var s = document.createElement("script");
  14. s.textContent = "(" + func + ")();";
  15. document.body.appendChild(s);
  16. setTimeout(function(){document.body.removeChild(s)}, 0);
  17. }
  18. RunInPage(function(){
  19. var PARENT = '<a class="comment-link comment-parent-link" href="">↑</a>';
  20. var $ = jQuery;
  21.  
  22. //dirty, DIRTY hack to wait for certain element to appear. -_-
  23. //But I have no idea how to do it right.
  24. function waitForSelector(selector, context, mustexist, callback) {
  25. if (($(selector, context).length>0) == mustexist)
  26. callback();
  27. else
  28. setTimeout(function(){waitForSelector(selector, context, mustexist, callback)}, 50);
  29. }
  30.  
  31. //short function for adding custom CSS rules. Why use Greasemonkey specific GM_setStyle() just for that?
  32. function addCSS(rule) {
  33. var styleElement = document.createElement("style");
  34. styleElement.type = "text/css";
  35. if (typeof styleElement.styleSheet !== 'undefined')
  36. styleElement.styleSheet.cssText = rule;
  37. else
  38. styleElement.appendChild(document.createTextNode(rule));
  39. document.getElementsByTagName("head")[0].appendChild(styleElement);
  40. }
  41.  
  42. addCSS([
  43. '.comment-parent-link {margin-left:10px;font-size:10pt}',
  44. ].join('/n'));
  45.  
  46. function hijackComments() {
  47. var oldLoadComments = comments['load'];
  48.  
  49. function newLoadComments(aElemTrigger) {
  50. var $parent = $(aElemTrigger).closest('.entry-comments');
  51. oldLoadComments.call(this,aElemTrigger);
  52. waitForSelector('.hcomment', $parent, true, function(){
  53. setParentLinks($parent);
  54. });
  55. }
  56.  
  57. comments['load'] = newLoadComments;
  58. }
  59.  
  60. function highlightComment(e) {
  61. if (! $(e.target).is('a.comment-parent-link')) return;
  62. var match = $(e.target).attr('href').match(/\#comment(\d+)$/);
  63. if (match) {
  64. $('body').find('.highlight').removeClass('highlight');
  65. $('#comment-'+match[1]).addClass('highlight');
  66. }
  67. }
  68. function setParentLinks($context) {
  69. $context.find('.hcomment').each(function(i,e){
  70. var $this = $(this);
  71. var $parent = $this.parents('.hcomment:eq(0)');
  72. if ($parent.length) {
  73. var $parentlink = $(PARENT);
  74. $parentlink.attr('href', '#'+$parent.find('a.comment-link:eq(0)').attr('name'));
  75. $parentlink.attr('title', $parent.find('.comment-text:eq(0)').text());
  76. $this.find('a.comment-link:eq(0)').after($parentlink);
  77. }
  78. });
  79. }
  80.  
  81. setParentLinks($('body'));
  82. hijackComments();
  83. $('body').click(highlightComment);
  84. });
  85. })();