GA forums tree

Позволяет "сворачивать" и "разворачивать" основные разделы форума "Глобальная авантюра" по принципу дерева.

  1. // ==UserScript==
  2. // @name GA forums tree
  3. // @namespace glav.su
  4. // @description Позволяет "сворачивать" и "разворачивать" основные разделы форума "Глобальная авантюра" по принципу дерева.
  5. // @include http://glav.su/forum/
  6. // @include https://glav.su/forum/
  7. // @version 1.1
  8. // @grant none
  9. // ==/UserScript==
  10. //Работа с Cookie
  11. function setCookie(name, value, options) {
  12. options = options || {};
  13. var expires = options.expires;
  14. if (typeof expires == "number" && expires) {
  15. var d = new Date();
  16. d.setTime(d.getTime() + expires * 1000);
  17. expires = options.expires = d;
  18. }
  19. if (expires && expires.toUTCString) {
  20. options.expires = expires.toUTCString();
  21. }
  22. value = encodeURIComponent(value);
  23. var updatedCookie = name + "=" + value;
  24. for (var propName in options) {
  25. updatedCookie += "; " + propName;
  26. var propValue = options[propName];
  27. if (propValue !== true) {
  28. updatedCookie += "=" + propValue;
  29. }
  30. }
  31. document.cookie = updatedCookie;
  32. }
  33. function getCookie(name) {
  34. var matches = document.cookie.match( new RegExp("(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)") );
  35. return matches ? decodeURIComponent(matches[1]) : undefined;
  36. }
  37. //Объект настроек
  38. var settings = ({
  39. cook: 'ga_forums_tree',
  40. values: {},
  41. load: function(){
  42. try{
  43. this.values = JSON.parse( getCookie(this.cook) );
  44. }catch(e){
  45. this.values = {};
  46. this.save();
  47. }
  48. return this;
  49. },
  50. save: function(){
  51. setCookie( this.cook, JSON.stringify(this.values), {expires: 100000000} );
  52. },
  53. set: function(key, value){
  54. this.values[key] = value;
  55. this.save();
  56. },
  57. get: function(key){
  58. if(typeof this.values[key] == 'undefined'){
  59. this.values[key] = 0;
  60. this.save();
  61. }
  62. return this.values[key];
  63. }
  64. }).load();
  65. //Форумы
  66. $.each( $(".blueHeader.f"), function(fidx, ftable){
  67. $(ftable).prev("br").hide();
  68. var ftd = $(ftable).find(".blueHeaderTitle.fItem");
  69. if( $(ftd).find("a").length === 0 ) return true;
  70. var fkey = $(ftd).find("a").attr("href").replace(/^https?:\/\/glav\.su\/forum\/(.*?)\/$/,"$1"),
  71. fvisible = +( settings.get(fkey) );
  72. if(fvisible === 0) $(ftable).next().hide();
  73. $(ftable).attr("fkey", fkey);
  74. $("<span>&nbsp;</span>").prependTo($(ftd));
  75. $('<span fvisible="'+fvisible+'" fkey="'+fkey+'">' + ['[+]','[-]'][fvisible] + '</span>')
  76. .css({ 'cursor': "pointer", 'font-family': "monospace" })
  77. .click(function(){
  78. var fkey = $(this).attr("fkey"), fvisible = +$(this).attr("fvisible");
  79. fvisible = +(!fvisible);
  80. $(this).html(['[+]','[-]'][fvisible]).attr("fvisible", fvisible);
  81. settings.set(fkey, fvisible);
  82. $(".blueHeader.f[fkey='"+fkey+"']").next().toggle();
  83. })
  84. .prependTo($(ftd));
  85. });