PTH User tagging

Tag, ignore, highlight, and change avatars for users on PTH

当前为 2017-02-20 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name PTH User tagging
  3. // @version 0.82
  4. // @description Tag, ignore, highlight, and change avatars for users on PTH
  5. // @author Chameleon
  6. // @include http*://redacted.ch/*
  7. // @grant none
  8. // @namespace https://greasyfork.org/users/87476
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. window.setTimeout(checkHeight.bind(undefined, document.body.clientHeight), 800);
  15.  
  16. if(window.location.href.indexOf('user.php?id=') != -1)
  17. {
  18. var username=document.getElementsByTagName('h2')[0].getElementsByTagName('a')[0].textContent;
  19. var a=document.createElement('a');
  20. a.innerHTML = '[User tags]';
  21. a.href='javascript:void(0);';
  22. a.addEventListener('click', openTags.bind(undefined, username, undefined), false);
  23. document.getElementsByClassName('linkbox')[0].appendChild(a);
  24. }
  25.  
  26. var posts=document.getElementsByClassName('forum_post');
  27. for(var i=0; i<posts.length; i++)
  28. {
  29. var p=posts[i];
  30. if(p.getAttribute('class').indexOf('preview_wrap') != -1)
  31. continue;
  32. var links=p.getElementsByTagName('td')[0].firstElementChild;
  33. var username=p.getElementsByTagName('strong')[0].getElementsByTagName('a')[0].textContent;
  34.  
  35. var a=document.createElement('a');
  36. a.href='javascript:void(0);';
  37. a.innerHTML='Tag';
  38. a.setAttribute('class', 'brackets');
  39. a.addEventListener('click', openTags.bind(undefined, username, p), false);
  40. links.appendChild(document.createTextNode(' - '));
  41. links.appendChild(a);
  42.  
  43. var img=p.getElementsByTagName('img')[0];
  44. if(img)
  45. {
  46. img.setAttribute('originalAvatar', img.src);
  47. }
  48. }
  49. /*
  50. var avatars=document.getElementsByClassName('avatar');
  51. for(var i=0; i<avatars.length; i++)
  52. {
  53. var avatar=avatars[i];
  54. addTagLinks(avatar);
  55. var img=avatar.getElementsByTagName('img')[0];
  56. if(img)
  57. {
  58. img.setAttribute('originalAvatar', img.src);
  59. }
  60. }*/
  61.  
  62. addTags();
  63. })();
  64.  
  65.  
  66. function checkHeight(height)
  67. {
  68. if(height != document.body.clientHeight)
  69. {
  70. pageResized();
  71. }
  72.  
  73. window.setTimeout(checkHeight.bind(undefined, document.body.clientHeight), 800);
  74. }
  75. /*
  76. function addTagLinks(avatar)
  77. {
  78. var tags=getTags();
  79.  
  80. var postTable=avatar.parentNode;
  81. while(postTable.tagName != 'TABLE')
  82. postTable=postTable.parentNode;
  83. if(postTable.getAttribute('id') == 'preview_wrap_0')
  84. return;
  85. var username=postTable.getElementsByTagName('strong')[0].textContent;
  86.  
  87. var id=postTable.getAttribute('id').split('post')[1];
  88.  
  89. var a=document.createElement('a');
  90. a.setAttribute('class', 'tagLink');
  91. a.setAttribute('postId', id);
  92. var place = avatar.getBoundingClientRect();
  93. var style='position: absolute; z-index: 50000000; top: '+(place.top+window.scrollY)+'px; left: '+(place.left+window.scrollX)+'px; width: '+avatar.clientWidth+'px;';
  94. style+='text-align: center; color: blue; background: rgba(200,200,200,0.8); border-radius: 0px 0px 10px 10px;';
  95. a.setAttribute('style', style);
  96.  
  97. a.innerHTML = 'Show user tags';
  98. a.href='javascript:void(0);';
  99. a.addEventListener('click', openTags.bind(undefined, username, postTable), false);
  100. document.body.appendChild(a);
  101. a.style.display='none';
  102.  
  103. avatar.addEventListener('mouseover', mouseOver.bind(undefined, a), false);
  104. avatar.addEventListener('mouseout', mouseOut.bind(undefined, avatar, a), false);
  105. }
  106. */
  107. function pageResized()
  108. {
  109. /*var tagLinks=document.getElementsByClassName('tagLink');
  110. for(var i=0; i<tagLinks.length; i++)
  111. {
  112. var t=tagLinks[i];
  113. var id=t.getAttribute('postId');
  114. var postTable=document.getElementById('post'+id);
  115. var avatar=postTable.getElementsByClassName('avatar')[0];
  116.  
  117. var place = avatar.getBoundingClientRect();
  118. var style='position: absolute; z-index: 50000000; top: '+(place.top+window.scrollY)+'px; left: '+(place.left+window.scrollX)+'px; width: '+avatar.clientWidth+'px;';
  119. style+='text-align: center; color: blue; background: rgba(200,200,200,0.8); border-radius: 0px 0px 10px 10px;';
  120. t.setAttribute('style', style);
  121. t.style.display='none';
  122. }*/
  123.  
  124. resetTags();
  125. addTags();
  126. }
  127.  
  128. function resetTags()
  129. {
  130. var ignoredQuotes=document.getElementsByClassName('toggleQuote');
  131. for(var i=0; i<ignoredQuotes.length; i++)
  132. {
  133. var ig=ignoredQuotes[i];
  134. ig.nextElementSibling.style.display='';
  135. ig.parentNode.removeChild(ig);
  136. }
  137.  
  138. var posts=document.getElementsByClassName('forum_post');
  139. for(var i=0; i<posts.length-1; i++)
  140. {
  141. var p=posts[i];
  142. var avatar=p.getElementsByClassName('avatar')[0];
  143.  
  144. var postTable=p;
  145.  
  146. if(postTable.getAttribute('id') == 'preview_wrap_0')
  147. continue;
  148. var u=postTable.getElementsByTagName('strong')[0].getElementsByTagName('a')[0];
  149. var username=u.textContent;
  150.  
  151.  
  152. var c=postTable.getElementsByClassName('user_title');
  153. if(c.length > 0)
  154. {
  155. var orig=c[0].getAttribute('original');
  156. if(orig)
  157. c[0].innerHTML=orig;
  158. }
  159.  
  160. if(avatar)
  161. {
  162. var img=avatar.getElementsByTagName('img')[0];
  163. if(img)
  164. {
  165. var orig=img.getAttribute('originalAvatar');
  166. if(orig)
  167. img.src=img.getAttribute('originalAvatar');
  168. }
  169. }
  170. u.setAttribute('style', '');
  171. postTable.setAttribute('style', '');
  172. var tr=postTable.getElementsByTagName('tr')[1];
  173. if(tr.getAttribute('stayHidden')!=="true")
  174. {
  175. tr.style.display='';
  176. }
  177. var id=postTable.getAttribute('id').split('post')[1];
  178. var tag=document.getElementById('tag'+id);
  179. if(tag)
  180. tag.parentNode.removeChild(tag);
  181. }
  182. var hardIgnores=document.getElementsByClassName('hardIgnoreLink');
  183. for(var i=0; i<hardIgnores.length; i++)
  184. {
  185. var h=hardIgnores[i];
  186. h.parentNode.removeChild(h);
  187. }
  188. }
  189.  
  190. function addTags()
  191. {
  192. var quotes=document.getElementsByTagName('blockquote');
  193. for(var i=0; i<quotes.length; i++)
  194. {
  195. var q=quotes[i];
  196. var username = q.previousElementSibling;
  197. if(username)
  198. {
  199. username=username.textContent.split(' ')[0];
  200. var user=getUser(username)[0];
  201. if(user.softIgnore || user.hardIgnore)
  202. {
  203. var a=document.createElement('a');
  204. a.href='javascript:void(0);';
  205. a.textContent='<Ignored>';
  206. a.addEventListener('click', toggleQuote.bind(undefined, a, q), false);
  207. a.setAttribute('class', 'toggleQuote');
  208. q.parentNode.insertBefore(a, q);
  209. if(q.getAttribute('unIgnored')=="true")
  210. {
  211. a.textContent='Ignore';
  212. }
  213. else
  214. q.style.display='none';
  215. }
  216. }
  217. }
  218.  
  219. var posts=document.getElementsByClassName('forum_post');
  220. for(var i=0; i<posts.length-1; i++)
  221. {
  222. var p=posts[i];
  223. var avatar=p.getElementsByClassName('avatar')[0];
  224.  
  225. var postTable=p;
  226.  
  227. if(postTable.getAttribute('id') == 'preview_wrap_0')
  228. continue;
  229. var u=postTable.getElementsByTagName('strong')[0].getElementsByTagName('a')[0];
  230. var username=u.textContent;
  231.  
  232. var user=getUser(username)[0];
  233. if(user.replacementAvatar && avatar)
  234. {
  235. avatar.getElementsByTagName('img')[0].src=user.replacementAvatar;
  236. }
  237. if(user.usernameColour)
  238. {
  239. var style=u.getAttribute('style');
  240. if(!style)
  241. style='';
  242. u.setAttribute('style', style+' color: '+user.usernameColour+';');
  243. }
  244. if(user.postHighlight)
  245. {
  246. var style=postTable.getAttribute('style');
  247. postTable.setAttribute('style', 'box-shadow: '+user.postHighlight+' 0 0 5px 1px !important;');
  248. }
  249. if(user.customTitle)
  250. {
  251. var c=postTable.getElementsByClassName('user_title');
  252. if(c.length > 0)
  253. c=c[0];
  254. else
  255. {
  256. c=document.createElement('span');
  257. c.setAttribute('class', 'user_title');
  258. var before=postTable.getElementsByClassName('time')[0];
  259. before.parentNode.insertBefore(c, before);
  260. }
  261. if(!c.getAttribute('original'))
  262. c.setAttribute('original', c.innerHTML);
  263.  
  264. c.innerHTML='('+user.customTitle+')';
  265. }
  266. if(user.tag && user.showTag)
  267. {
  268. var div=document.createElement('div');
  269. var id=postTable.getAttribute('id').split('post')[1];
  270. div.setAttribute('id', 'tag'+id);
  271. div.innerHTML = user.tag.replace(/\n/g,'<br />')+' ';
  272. if(!user.showTagInHeader)
  273. {
  274. var before=document.getElementById('bar'+id).firstElementChild;
  275. before.parentNode.insertBefore(div, before);
  276. div.setAttribute('style', 'display: inline-block; margin-right: 5px;');
  277. div.setAttribute('class', 'r10');
  278. }
  279. else
  280. {
  281. var first;
  282. if(!avatar)
  283. {
  284. avatar=postTable;
  285. first=avatar;
  286. }
  287. else
  288. first=avatar.firstElementChild;
  289. var place = postTable.getBoundingClientRect();
  290. var width=300;
  291. var left=place.left+window.scrollX-width-20;
  292. if(left<0)
  293. left=0;
  294. var style='position: absolute; z-index: 1001; top: '+(place.top+window.scrollY)+'px; left: '+left+'px; max-width: '+width+'px; text-align: center; color: white; background: rgba(20,20,20,0.7); border-radius: 20px 0px 0px 20px;';
  295. style+='font-size: large; box-shadow: inset '+(user.postHighlight ? user.postHighlight : 'black')+' 0 0 20px 0; padding: 10px;';
  296. div.setAttribute('style', style);
  297. document.body.appendChild(div);
  298. var avatarHeight=first.clientHeight;
  299. var top=place.top+window.scrollY+((avatarHeight-div.clientHeight)/2);
  300. div.style.top=top+'px';
  301. if(div.clientWidth < width)
  302. {
  303. left=place.left+window.scrollX-div.clientWidth;
  304. if(left<0)
  305. left=0;
  306. div.style.left=left+'px';
  307. }
  308. }
  309. }
  310. if(user.softIgnore)
  311. {
  312. postTable.getElementsByTagName('tr')[1].style.display='none';
  313. }
  314. if(user.hardIgnore)
  315. {
  316. var a=document.createElement('a');
  317. var hr=document.createElement('hr');
  318. hr.setAttribute('title', username);
  319. a.appendChild(hr);
  320. a.setAttribute('class', 'hardIgnoreLink');
  321. a.href=postTable.getElementsByTagName('strong')[0].getElementsByTagName('a')[0].href;
  322. postTable.parentNode.insertBefore(a, postTable);
  323. postTable.style.display='none';
  324. }
  325. }
  326. }
  327.  
  328. function toggleQuote(a, q)
  329. {
  330. if(a.innerHTML.indexOf('Ignored') != -1)
  331. {
  332. a.textContent = 'Ignore';
  333. q.style.display='';
  334. q.setAttribute('unIgnored', "true");
  335. }
  336. else
  337. {
  338. a.textContent = '<Ignored>';
  339. q.style.display='none';
  340. q.setAttribute('unIgnored', "false");
  341. }
  342. }
  343.  
  344. function openTags(username, postTable)
  345. {
  346. var div=document.getElementById('chameleonTagsDiv');
  347. if(!div)
  348. {
  349. div=document.createElement('div');
  350. div.setAttribute('id', 'chameleonTagsDiv');
  351. document.body.appendChild(div);
  352. div.setAttribute('style', 'position: fixed; top: 20px; margin: auto; left: 0; right: 0; text-align: center; background: rgba(0,0,0,0.7); color: white; width: 80%; z-index: 1000;');
  353. }
  354. div.innerHTML = '<h2>'+username+'\'s Tags<br />';
  355.  
  356. var user=getUser(username)[0];
  357.  
  358. var input=document.createElement('input');
  359. div.appendChild(input);
  360. input.placeholder='Replacement avatar URL';
  361. input.value = user.replacementAvatar ? user.replacementAvatar : '';
  362. input.addEventListener('change', changeTags.bind(undefined, div, username, postTable, input), false);
  363.  
  364. div.appendChild(document.createElement('br'));
  365.  
  366. var input=document.createElement('input');
  367. div.appendChild(input);
  368. input.placeholder='Replacement custom title';
  369. input.value = user.customTitle ? user.customTitle : '';
  370. input.addEventListener('change', changeTags.bind(undefined, div, username, postTable, input), false);
  371.  
  372. div.appendChild(document.createElement('br'));
  373.  
  374. var input=document.createElement('input');
  375. div.appendChild(input);
  376. input.placeholder='Post highlight colour';
  377. input.value = user.postHighlight ? user.postHighlight : '';
  378. input.addEventListener('change', changeTags.bind(undefined, div, username, postTable, input), false);
  379.  
  380. div.appendChild(document.createElement('br'));
  381.  
  382. var input=document.createElement('input');
  383. div.appendChild(input);
  384. input.placeholder='Username colour';
  385. input.value = user.usernameColour ? user.usernameColour : '';
  386. input.addEventListener('change', changeTags.bind(undefined, div, username, postTable, input), false);
  387.  
  388. div.appendChild(document.createElement('br'));
  389.  
  390. var input=document.createElement('textarea');
  391. input.setAttribute('id', 'tagTextarea');
  392. div.appendChild(input);
  393. input.setAttribute('style', 'text-align: center; border: none;');
  394. input.placeholder='Tag';
  395. input.value = user.tag ? user.tag : '';
  396. resize('tagTextarea');
  397. input.addEventListener('keyup', resize.bind(undefined, 'tagTextarea'), false);
  398. input.addEventListener('change', changeTags.bind(undefined, div, username, postTable, input), false);
  399.  
  400. div.appendChild(document.createElement('br'));
  401.  
  402. var a=document.createElement('a');
  403. div.appendChild(a);
  404. a.innerHTML = 'Show tag: '+(user.showTag ? 'On' : 'Off');
  405. a.href='javascript:void(0);';
  406. a.addEventListener('click', changeTags.bind(undefined, div, username, postTable, a), false);
  407.  
  408. div.appendChild(document.createElement('br'));
  409.  
  410. var a=document.createElement('a');
  411. div.appendChild(a);
  412. a.innerHTML = 'Show tag left of avatar: '+(user.showTagInHeader ? 'On' : 'Off');
  413. a.href='javascript:void(0);';
  414. a.addEventListener('click', changeTags.bind(undefined, div, username, postTable, a), false);
  415.  
  416. div.appendChild(document.createElement('br'));
  417.  
  418. var a=document.createElement('a');
  419. div.appendChild(a);
  420. a.innerHTML = 'Soft ignore: '+(user.softIgnore ? 'On' : 'Off');
  421. a.href='javascript:void(0);';
  422. a.addEventListener('click', changeTags.bind(undefined, div, username, postTable, a), false);
  423.  
  424. div.appendChild(document.createElement('br'));
  425.  
  426. var a=document.createElement('a');
  427. div.appendChild(a);
  428. a.innerHTML = 'Hard ignore: '+(user.hardIgnore ? 'On' : 'Off');
  429. a.href='javascript:void(0);';
  430. a.addEventListener('click', changeTags.bind(undefined, div, username, postTable, a), false);
  431.  
  432. div.appendChild(document.createElement('br'));
  433.  
  434. var a=document.createElement('a');
  435. div.appendChild(a);
  436. a.innerHTML = 'Save';
  437. a.href='javascript:void(0);';
  438. a.addEventListener('click', saveAndClose.bind(undefined, div, username, postTable), false);
  439. }
  440.  
  441. function changeTags(div, username, table, a)
  442. {
  443. var user=getUser(username);
  444. var index=user[1];
  445. user=user[0];
  446.  
  447. var inputs=div.getElementsByTagName('input');
  448. user.replacementAvatar = inputs[0].value;
  449. user.customTitle = inputs[1].value;
  450. user.postHighlight = inputs[2].value;
  451. user.usernameColour = inputs[3].value;
  452.  
  453. var textareas=div.getElementsByTagName('textarea');
  454. user.tag=textareas[0].value;
  455.  
  456. var as=div.getElementsByTagName('a');
  457. if(as[0] == a)
  458. {
  459. if(a.innerHTML.indexOf('On') != -1)
  460. user.showTag=false;
  461. else
  462. user.showTag=true;
  463. }
  464. if(as[1] == a)
  465. {
  466. if(a.innerHTML.indexOf('On') != -1)
  467. user.showTagInHeader=false;
  468. else
  469. user.showTagInHeader=true;
  470. }
  471. if(as[2] == a)
  472. {
  473. if(a.innerHTML.indexOf('On') != -1)
  474. user.softIgnore=false;
  475. else
  476. user.softIgnore=true;
  477. }
  478. if(as[3] == a)
  479. {
  480. if(a.innerHTML.indexOf('On') != -1)
  481. user.hardIgnore=false;
  482. else
  483. user.hardIgnore=true;
  484. }
  485.  
  486. var tags=getTags();
  487. if(index != -1)
  488. tags[index]=user;
  489. else
  490. {
  491. user.username=username;
  492. tags.push(user);
  493. }
  494. window.localStorage.userTags = JSON.stringify(tags);
  495.  
  496. openTags(username, table);
  497. }
  498.  
  499. function saveAndClose(div, username, table)
  500. {
  501. resetTags();
  502. addTags();
  503. div.parentNode.removeChild(div);
  504. }
  505.  
  506. /*
  507. function mouseOver(a)
  508. {
  509. a.style.display = 'initial';
  510. }
  511.  
  512. function mouseOut(avatar, a, event)
  513. {
  514. if(event.relatedTarget == avatar || event.relatedTarget == a)
  515. return;
  516. a.style.display = 'none';
  517. }*/
  518.  
  519. function getUser(username)
  520. {
  521. var tags=getTags();
  522. for(var i=0; i<tags.length; i++)
  523. {
  524. var t=tags[i];
  525. if(t.username === username)
  526. return [t, i];
  527. }
  528. return [{}, -1];
  529. }
  530.  
  531. function getTags()
  532. {
  533. var tags = window.localStorage.userTags;
  534. if(!tags)
  535. {
  536. tags = [];
  537. }
  538. else
  539. tags = JSON.parse(tags);
  540. return tags;
  541. }