tiebaMyLike

我喜欢的贴吧一览

  1. // ==UserScript==
  2. // @name tiebaMyLike
  3. // @description 我喜欢的贴吧一览
  4. // @include http://tieba.baidu.com/f?kw=*
  5. // @include http://tieba.baidu.com/f?ie=gbk&kw=*
  6. // @include http://tieba.baidu.com/f?ie=utf-8&kw=*
  7. // @include http://tieba.baidu.com/p/*
  8. // @include http://tieba.baidu.com/f?ct=*
  9. // @include http://tieba.baidu.com/f?kz=*
  10. // @include http://tieba.baidu.com/f?tp=0&kw=*
  11. // @include http://tieba.baidu.com/f/good?kw=*
  12. // @icon http://tb.himg.baidu.com/sys/portraitn/item/4e2ed7f8bbb3d4f2c2d2bb21
  13. // @author congxz6688
  14. // @version 2013.4.3.0
  15. // @namespace https://greasyfork.org/scripts/161
  16. // ==/UserScript==
  17.  
  18.  
  19. //此处供用户额外添加自己喜欢的吧,前10个有效
  20. //一般说来,此处添加的吧应该是那种经验排名在20以后,无法出现在正常列表中的吧
  21. //贴吧名称不要带后面的“吧”字,就象下面的例子一样,用小写的双引号括起来,再用小写的逗号相互隔开
  22. var addByUser = ["贴吧例一", "贴吧例二"];
  23.  
  24. /**********************以下部分不要随意修改**********************/
  25.  
  26. var userSetNum = 20;
  27. var signCSS = "";
  28. signCSS += "#mylikeDiv{margin:-10px 0px -16px 0px!important;} .fonColor{color:#1D53BF} .useColor{color:#8B0000;}";
  29. signCSS += "#mylikeDiv td{padding: 3px !important;} #likeTitle,#resetMylike{display:inline-block; cursor:pointer; width:50%; padding:9px 0px; border-bottom:2px solid #246DDA;}";
  30. signCSS += ".likeTd{padding:0px 8px;} .userAdd{height:24px; padding:0px 12px 0px 8px;}#replaceUl{font-size:12px; margin:10px}";
  31. signCSS += "#floatLikeDiv{border:1px solid grey; z-index:999; padding:8px 10px 10px 10px;background-color:white;}";
  32. GM_addStyle(signCSS);
  33. if (document.getElementById("frs_old_version")) {
  34. GM_addStyle("#mylikeDiv{display:none;}")
  35. }
  36. var yuy = new Date();
  37. re = yuy.getTime() + 28800000;
  38. yuy.setTime(re);
  39. var fulltime = yuy.getUTCFullYear() + "/" + (yuy.getUTCMonth() + 1) + "/" + yuy.getUTCDate();
  40. var userSignName = unsafeWindow.PageData.user.name;
  41. var itieba_id = unsafeWindow.PageData.user.itieba_id;
  42. if (userSignName != GM_getValue('currentUser', '')) {
  43. GM_setValue('currentUser', userSignName);
  44. getMylikeTiebas;
  45. }
  46. //函数 绝对定位
  47. function getElementTop(element) {
  48. var actualTop = element.offsetTop;
  49. var current = element.offsetParent;
  50. while (current !== null) {
  51. actualTop += current.offsetTop;
  52. current = current.offsetParent;
  53. }
  54. return actualTop;
  55. }
  56. function getElementRight(element) {
  57. var actualRight =document.body.offsetWidth - element.offsetLeft - 180; //此处可微调悬浮列表框的左右位置
  58. return actualRight;
  59. }
  60. //创建列表显示区
  61. var mylikeDiv = document.createElement("div");
  62. mylikeDiv.setAttribute("class", "region_3");
  63. mylikeDiv.id = "mylikeDiv";
  64.  
  65. var likeTitle = document.createElement("div");
  66. likeTitle.setAttribute("class", "region_title");
  67. likeTitle.id = "likeTitle";
  68. likeTitle.innerHTML = "我喜欢的贴吧";
  69. likeTitle.addEventListener("click", function () {
  70. window.open("http://tieba.baidu.com/i/" + itieba_id + "/forum");
  71. }, false);
  72. mylikeDiv.appendChild(likeTitle);
  73.  
  74. var resetMylike = document.createElement("div");
  75. resetMylike.setAttribute("class", "region_title");
  76. resetMylike.id = "resetMylike";
  77. resetMylike.innerHTML = "重置";
  78. resetMylike.addEventListener("click", getMylikeTiebas, false)
  79. mylikeDiv.appendChild(resetMylike);
  80.  
  81. var likeListDiv = document.createElement("div");
  82. likeListDiv.setAttribute("class", "region_cnt");
  83. mylikeDiv.appendChild(likeListDiv);
  84.  
  85. var Tds1 = [];
  86. var Tds2 = [];
  87. //建表函数
  88. function creaseTable(UrlLength, cons) {
  89. Tds1 = [];
  90. Tds2 = [];
  91. var tablepp = document.createElement("table");
  92. tablepp.setAttribute("class", "likeTable");
  93. tablepp.setAttribute("width", "100%");
  94. var trs = [];
  95. for (ly = 0; ly < Math.ceil(UrlLength / cons); ly++) {
  96. var tr = document.createElement("tr");
  97. mmd = trs.push(tr);
  98. tablepp.appendChild(tr);
  99. }
  100. for (ls = 0; ls < UrlLength; ls++) {
  101. var td = document.createElement("td")
  102. if (cons == 1) {
  103. td.setAttribute("class", "userAdd");
  104. wq = Tds2.push(td);
  105. } else {
  106. td.setAttribute("class", "likeTd");
  107. wq = Tds1.push(td);
  108. }
  109. trs[Math.floor(ls / cons)].appendChild(td);
  110. }
  111. return tablepp
  112. }
  113.  
  114. //右上角“我爱逛的贴吧”相关替换函数
  115. function openMylikeList(e) {
  116. if (!document.getElementById("floatLikeDiv")) {
  117. de = e.target;
  118. var thisTop = getElementTop(document.getElementsByClassName("nav_right")[0]) + 28;
  119. var thisRight = getElementRight(de);
  120. GM_addStyle("#floatLikeDiv{position:absolute; right:" + thisRight + "px; top:" + thisTop + "px}");
  121. var floatLikeDiv = document.createElement("div");
  122. floatLikeDiv.id = "floatLikeDiv";
  123. var fTable = document.createElement("table");
  124. floatLikeDiv.appendChild(fTable);
  125. var fTr = document.createElement("tr");
  126. fTable.appendChild(fTr);
  127. var fTd1 = document.createElement("td");
  128. var fTd2 = document.createElement("td");
  129. fTr.appendChild(fTd1);
  130. fTr.appendChild(fTd2);
  131. var likeTableFloat = document.getElementsByClassName("likeTable")[0].cloneNode(true);
  132. fTd2.appendChild(likeTableFloat);
  133. var allb = JSON.parse(GM_getValue('todayMylike', "{}"));
  134. var allbas = allb[userSignName] ? allb[userSignName] : [];
  135. var huuw = addByUser.deleteThe("贴吧例一").deleteThe("贴吧例二").deleteRepeatWith(allbas);
  136. if (huuw.length > 0) {
  137. var duur = huuw.length <= 10 ? huuw.length : 10;
  138. var floatLg = Math.max(duur, Math.ceil(allbas.length / 2));
  139. var userAddFt = creaseTable(floatLg, 1);
  140. for (vv = 0; vv < duur; vv++) {
  141. var anch = document.createElement("a");
  142. anch.href = "http://tieba.baidu.com/f?kw=" + huuw[vv];
  143. anch.title = huuw[vv];
  144. anch.target = "_blank";
  145. anch.innerHTML = '<fon class="useColor">' + huuw[vv].reComLength() + '</fon>';
  146. Tds2[vv].appendChild(anch);
  147. }
  148. fTd1.appendChild(userAddFt);
  149. }
  150. floatLikeDiv.addEventListener("mouseleave", closeMylikeList, false)
  151. document.querySelector(".nav_right").appendChild(floatLikeDiv);
  152. }
  153. }
  154. function closeMylikeList() {
  155. document.getElementById("floatLikeDiv").parentNode.removeChild(document.getElementById("floatLikeDiv"));
  156. }
  157. function addFloatMylike() {
  158. var replaceUl = document.createElement("li");
  159. replaceUl.id = "replaceUl";
  160. replaceUl.innerHTML = "<font color='white'>我爱逛的贴吧</font>";
  161. replaceUl.addEventListener("click", function () {
  162. window.open("http://tieba.baidu.com/i/" + itieba_id + "/forum");
  163. }, false);
  164. replaceUl.addEventListener("mouseover", openMylikeList, true);
  165. var tempNode = document.querySelector(".li_often_forum");
  166. document.querySelector(".nav_right").insertBefore(replaceUl, tempNode);
  167. tempNode.parentNode.removeChild(tempNode);
  168. }
  169. //右侧栏相关添加部分
  170. function addAsideDiv() {
  171. if (document.getElementById("aside")) {
  172. var parentDiv = document.getElementById("aside");
  173. } else if (document.getElementById("j_aside")) {
  174. var parentDiv = document.getElementById("j_aside");
  175. } else if (document.querySelector(".right_section")) {
  176. var parentDiv = document.querySelector(".right_section");
  177. }
  178. parentDiv.insertBefore(mylikeDiv, document.getElementById("balv_mod").nextSibling);
  179. loadLiketieba();
  180. }
  181. //吧名长度计算
  182. String.prototype.reComLength = function () {
  183. var yn = 0;
  184. var kuu = "";
  185. for (w in this) {
  186. if (w < this.length) {
  187. if (/[a-zA-Z0-9]/.exec(this[w])) {
  188. yn += 1;
  189. } else {
  190. yn += 2;
  191. }
  192. if (yn < 13) {
  193. kuu += this[w];
  194. }
  195. }
  196. }
  197. var uui = yn > 15 ? kuu + "..." : this;
  198. return uui;
  199. }
  200. //数组中删除特定元素
  201. Array.prototype.deleteThe = function (v) {
  202. var ra = [];
  203. for (w = 0; w < this.length; w++) {
  204. if (this[w] != v) {
  205. foo = ra.push(this[w]);
  206. }
  207. }
  208. return ra;
  209. }
  210. //数组中删除与另一数组重复的元素
  211. Array.prototype.deleteRepeatWith = function (v) {
  212. var ra = [];
  213. for (w = 0; w < this.length; w++) {
  214. if (v.indexOf(this[w]) == -1){
  215. foo = ra.push(this[w]);
  216. }
  217. }
  218. return ra;
  219. }
  220. //载入贴吧列表进表格
  221. function loadLiketieba() {
  222. var allb = JSON.parse(GM_getValue('todayMylike', "{}"));
  223. if (allb[userSignName]) {
  224. allbaa = allb[userSignName];
  225. newTable = creaseTable(allbaa.length, 2);
  226. likeListDiv.appendChild(newTable);
  227. for (s = 0; s < allbaa.length; s++) {
  228. var anch = document.createElement("a");
  229. anch.href = "http://tieba.baidu.com/f?kw=" + allbaa[s];
  230. anch.title = allbaa[s];
  231. anch.target = "_blank";
  232. anch.innerHTML = '<fon class="fonColor">' + allbaa[s].reComLength() + '</fon>';
  233. Tds1[s].appendChild(anch);
  234. }
  235. }
  236. }
  237. //获取顺序列表
  238. function getMylikeTiebas() {
  239. GM_setValue('todayMylike', '{}');
  240. likeListDiv.innerHTML = "";
  241. GM_xmlhttpRequest({
  242. method : 'GET',
  243. synchronous : true,
  244. url : "http://tieba.baidu.com/f/like/mylike",
  245. onload : function (reDetails) {
  246. var wholeObject = {};
  247. var tempArrey = [];
  248. var simTxt = reDetails.responseText;
  249. var auDoc = document.implementation.createDocument("", "", null);
  250. var auElem = document.createElement('div');
  251. auElem.innerHTML = simTxt;
  252. auDoc.appendChild(auElem);
  253. var bass = auDoc.querySelectorAll("tr>td:first-child>a");
  254. if (userSetNum > 20 || userSetNum < 10) {
  255. userSetNum = 10;
  256. }
  257. ListLength = (bass.length >= userSetNum) ? userSetNum : bass.length;
  258. for (x = 0; x < ListLength; x++) {
  259. var tempA = tempArrey.push(bass[x].innerHTML);
  260. }
  261. wholeObject.date = fulltime;
  262. wholeObject[userSignName] = tempArrey;
  263. GM_setValue('todayMylike', JSON.stringify(wholeObject));
  264. loadLiketieba();
  265. }
  266. })
  267. }
  268.  
  269. var allba = JSON.parse(GM_getValue('todayMylike', "{}"));
  270. if (!allba[userSignName] || allba.date != fulltime || allba[userSignName].length != userSetNum) {
  271. getMylikeTiebas();
  272. }
  273. addAsideDiv();
  274. if (document.querySelector(".li_often_forum")) {
  275. addFloatMylike();
  276. }