PTH User tagging

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

当前为 2017-01-11 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name PTH User tagging
  3. // @version 0.8
  4. // @description Tag, ignore, highlight, and change avatars for users on PTH
  5. // @author Chameleon
  6. // @include http*://passtheheadphones.me/*
  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. console.log(tr.getAttribute('stayHidden')!=="true");
  176. tr.style.display='';
  177. }
  178. var id=postTable.getAttribute('id').split('post')[1];
  179. var tag=document.getElementById('tag'+id);
  180. if(tag)
  181. tag.parentNode.removeChild(tag);
  182. }
  183. var hardIgnores=document.getElementsByClassName('hardIgnoreLink');
  184. for(var i=0; i<hardIgnores.length; i++)
  185. {
  186. var h=hardIgnores[i];
  187. h.parentNode.removeChild(h);
  188. }
  189. }
  190.  
  191. function addTags()
  192. {
  193. var quotes=document.getElementsByTagName('blockquote');
  194. for(var i=0; i<quotes.length; i++)
  195. {
  196. var q=quotes[i];
  197. var username = q.previousElementSibling;
  198. if(username)
  199. {
  200. username=username.textContent.split(' ')[0];
  201. var user=getUser(username)[0];
  202. if(user.softIgnore || user.hardIgnore)
  203. {
  204. var a=document.createElement('a');
  205. a.href='javascript:void(0);';
  206. a.textContent='<Ignored>';
  207. a.addEventListener('click', toggleQuote.bind(undefined, a, q), false);
  208. a.setAttribute('class', 'toggleQuote');
  209. q.parentNode.insertBefore(a, q);
  210. if(q.getAttribute('unIgnored')=="true")
  211. {
  212. a.textContent='Ignore';
  213. }
  214. else
  215. q.style.display='none';
  216. }
  217. }
  218. }
  219.  
  220. var posts=document.getElementsByClassName('forum_post');
  221. for(var i=0; i<posts.length-1; i++)
  222. {
  223. var p=posts[i];
  224. var avatar=p.getElementsByClassName('avatar')[0];
  225.  
  226. var postTable=p;
  227.  
  228. if(postTable.getAttribute('id') == 'preview_wrap_0')
  229. continue;
  230. var u=postTable.getElementsByTagName('strong')[0].getElementsByTagName('a')[0];
  231. var username=u.textContent;
  232.  
  233. var user=getUser(username)[0];
  234. if(user.replacementAvatar && avatar)
  235. {
  236. avatar.getElementsByTagName('img')[0].src=user.replacementAvatar;
  237. }
  238. if(user.usernameColour)
  239. {
  240. var style=u.getAttribute('style');
  241. if(!style)
  242. style='';
  243. u.setAttribute('style', style+' color: '+user.usernameColour+';');
  244. }
  245. if(user.postHighlight)
  246. {
  247. var style=postTable.getAttribute('style');
  248. postTable.setAttribute('style', 'box-shadow: '+user.postHighlight+' 0 0 5px 1px !important;');
  249. }
  250. if(user.customTitle)
  251. {
  252. var c=postTable.getElementsByClassName('user_title');
  253. if(c.length > 0)
  254. c=c[0];
  255. else
  256. {
  257. c=document.createElement('span');
  258. c.setAttribute('class', 'user_title');
  259. var before=postTable.getElementsByClassName('time')[0];
  260. before.parentNode.insertBefore(c, before);
  261. }
  262. if(!c.getAttribute('original'))
  263. c.setAttribute('original', c.innerHTML);
  264.  
  265. c.innerHTML='('+user.customTitle+')';
  266. }
  267. if(user.tag && user.showTag)
  268. {
  269. var div=document.createElement('div');
  270. var id=postTable.getAttribute('id').split('post')[1];
  271. div.setAttribute('id', 'tag'+id);
  272. div.innerHTML = user.tag.replace(/\n/g,'<br />')+' ';
  273. if(!user.showTagInHeader)
  274. {
  275. var before=document.getElementById('bar'+id).firstElementChild;
  276. before.parentNode.insertBefore(div, before);
  277. div.setAttribute('style', 'display: inline-block; margin-right: 5px;');
  278. div.setAttribute('class', 'r10');
  279. }
  280. else
  281. {
  282. var first;
  283. if(!avatar)
  284. {
  285. avatar=postTable;
  286. first=avatar;
  287. }
  288. else
  289. first=avatar.firstElementChild;
  290. var place = postTable.getBoundingClientRect();
  291. var width=300;
  292. var left=place.left+window.scrollX-width-20;
  293. if(left<0)
  294. left=0;
  295. 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;';
  296. style+='font-size: large; box-shadow: inset '+(user.postHighlight ? user.postHighlight : 'black')+' 0 0 20px 0; padding: 10px;';
  297. div.setAttribute('style', style);
  298. document.body.appendChild(div);
  299. var avatarHeight=first.clientHeight;
  300. var top=place.top+window.scrollY+((avatarHeight-div.clientHeight)/2);
  301. div.style.top=top+'px';
  302. if(div.clientWidth < width)
  303. {
  304. left=place.left+window.scrollX-div.clientWidth;
  305. if(left<0)
  306. left=0;
  307. div.style.left=left+'px';
  308. }
  309. }
  310. }
  311. if(user.softIgnore)
  312. {
  313. postTable.getElementsByTagName('tr')[1].style.display='none';
  314. }
  315. if(user.hardIgnore)
  316. {
  317. var a=document.createElement('a');
  318. var hr=document.createElement('hr');
  319. hr.setAttribute('title', username);
  320. a.appendChild(hr);
  321. a.setAttribute('class', 'hardIgnoreLink');
  322. a.href=postTable.getElementsByTagName('strong')[0].getElementsByTagName('a')[0].href;
  323. postTable.parentNode.insertBefore(a, postTable);
  324. postTable.style.display='none';
  325. }
  326. }
  327. }
  328.  
  329. function toggleQuote(a, q)
  330. {
  331. if(a.innerHTML.indexOf('Ignored') != -1)
  332. {
  333. a.textContent = 'Ignore';
  334. q.style.display='';
  335. q.setAttribute('unIgnored', "true");
  336. }
  337. else
  338. {
  339. a.textContent = '<Ignored>';
  340. q.style.display='none';
  341. q.setAttribute('unIgnored', "false");
  342. }
  343. }
  344.  
  345. function openTags(username, postTable)
  346. {
  347. var div=document.getElementById('chameleonTagsDiv');
  348. if(!div)
  349. {
  350. div=document.createElement('div');
  351. div.setAttribute('id', 'chameleonTagsDiv');
  352. document.body.appendChild(div);
  353. 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;');
  354. }
  355. div.innerHTML = '<h2>'+username+'\'s Tags<br />';
  356.  
  357. var user=getUser(username)[0];
  358.  
  359. var input=document.createElement('input');
  360. div.appendChild(input);
  361. input.placeholder='Replacement avatar URL';
  362. input.value = user.replacementAvatar ? user.replacementAvatar : '';
  363. input.addEventListener('change', changeTags.bind(undefined, div, username, postTable, input), false);
  364.  
  365. div.appendChild(document.createElement('br'));
  366.  
  367. var input=document.createElement('input');
  368. div.appendChild(input);
  369. input.placeholder='Replacement custom title';
  370. input.value = user.customTitle ? user.customTitle : '';
  371. input.addEventListener('change', changeTags.bind(undefined, div, username, postTable, input), false);
  372.  
  373. div.appendChild(document.createElement('br'));
  374.  
  375. var input=document.createElement('input');
  376. div.appendChild(input);
  377. input.placeholder='Post highlight colour';
  378. input.value = user.postHighlight ? user.postHighlight : '';
  379. input.addEventListener('change', changeTags.bind(undefined, div, username, postTable, input), false);
  380.  
  381. div.appendChild(document.createElement('br'));
  382.  
  383. var input=document.createElement('input');
  384. div.appendChild(input);
  385. input.placeholder='Username colour';
  386. input.value = user.usernameColour ? user.usernameColour : '';
  387. input.addEventListener('change', changeTags.bind(undefined, div, username, postTable, input), false);
  388.  
  389. div.appendChild(document.createElement('br'));
  390.  
  391. var input=document.createElement('textarea');
  392. input.setAttribute('id', 'tagTextarea');
  393. div.appendChild(input);
  394. input.setAttribute('style', 'text-align: center; border: none;');
  395. input.placeholder='Tag';
  396. input.value = user.tag ? user.tag : '';
  397. resize('tagTextarea');
  398. input.addEventListener('keyup', resize.bind(undefined, 'tagTextarea'), false);
  399. input.addEventListener('change', changeTags.bind(undefined, div, username, postTable, input), false);
  400.  
  401. div.appendChild(document.createElement('br'));
  402.  
  403. var a=document.createElement('a');
  404. div.appendChild(a);
  405. a.innerHTML = 'Show tag: '+(user.showTag ? 'On' : 'Off');
  406. a.href='javascript:void(0);';
  407. a.addEventListener('click', changeTags.bind(undefined, div, username, postTable, a), false);
  408.  
  409. div.appendChild(document.createElement('br'));
  410.  
  411. var a=document.createElement('a');
  412. div.appendChild(a);
  413. a.innerHTML = 'Show tag left of avatar: '+(user.showTagInHeader ? 'On' : 'Off');
  414. a.href='javascript:void(0);';
  415. a.addEventListener('click', changeTags.bind(undefined, div, username, postTable, a), false);
  416.  
  417. div.appendChild(document.createElement('br'));
  418.  
  419. var a=document.createElement('a');
  420. div.appendChild(a);
  421. a.innerHTML = 'Soft ignore: '+(user.softIgnore ? 'On' : 'Off');
  422. a.href='javascript:void(0);';
  423. a.addEventListener('click', changeTags.bind(undefined, div, username, postTable, a), false);
  424.  
  425. div.appendChild(document.createElement('br'));
  426.  
  427. var a=document.createElement('a');
  428. div.appendChild(a);
  429. a.innerHTML = 'Hard ignore: '+(user.hardIgnore ? 'On' : 'Off');
  430. a.href='javascript:void(0);';
  431. a.addEventListener('click', changeTags.bind(undefined, div, username, postTable, a), false);
  432.  
  433. div.appendChild(document.createElement('br'));
  434.  
  435. var a=document.createElement('a');
  436. div.appendChild(a);
  437. a.innerHTML = 'Save';
  438. a.href='javascript:void(0);';
  439. a.addEventListener('click', saveAndClose.bind(undefined, div, username, postTable), false);
  440. }
  441.  
  442. function changeTags(div, username, table, a)
  443. {
  444. var user=getUser(username);
  445. var index=user[1];
  446. user=user[0];
  447.  
  448. var inputs=div.getElementsByTagName('input');
  449. user.replacementAvatar = inputs[0].value;
  450. user.customTitle = inputs[1].value;
  451. user.postHighlight = inputs[2].value;
  452. user.usernameColour = inputs[3].value;
  453.  
  454. var textareas=div.getElementsByTagName('textarea');
  455. user.tag=textareas[0].value;
  456.  
  457. var as=div.getElementsByTagName('a');
  458. if(as[0] == a)
  459. {
  460. if(a.innerHTML.indexOf('On') != -1)
  461. user.showTag=false;
  462. else
  463. user.showTag=true;
  464. }
  465. if(as[1] == a)
  466. {
  467. if(a.innerHTML.indexOf('On') != -1)
  468. user.showTagInHeader=false;
  469. else
  470. user.showTagInHeader=true;
  471. }
  472. if(as[2] == a)
  473. {
  474. if(a.innerHTML.indexOf('On') != -1)
  475. user.softIgnore=false;
  476. else
  477. user.softIgnore=true;
  478. }
  479. if(as[3] == a)
  480. {
  481. if(a.innerHTML.indexOf('On') != -1)
  482. user.hardIgnore=false;
  483. else
  484. user.hardIgnore=true;
  485. }
  486.  
  487. var tags=getTags();
  488. if(index != -1)
  489. tags[index]=user;
  490. else
  491. {
  492. user.username=username;
  493. tags.push(user);
  494. }
  495. window.localStorage.userTags = JSON.stringify(tags);
  496.  
  497. openTags(username, table);
  498. }
  499.  
  500. function saveAndClose(div, username, table)
  501. {
  502. resetTags();
  503. addTags();
  504. div.parentNode.removeChild(div);
  505. }
  506.  
  507. /*
  508. function mouseOver(a)
  509. {
  510. a.style.display = 'initial';
  511. }
  512.  
  513. function mouseOut(avatar, a, event)
  514. {
  515. if(event.relatedTarget == avatar || event.relatedTarget == a)
  516. return;
  517. a.style.display = 'none';
  518. }*/
  519.  
  520. function getUser(username)
  521. {
  522. var tags=getTags();
  523. for(var i=0; i<tags.length; i++)
  524. {
  525. var t=tags[i];
  526. if(t.username === username)
  527. return [t, i];
  528. }
  529. return [{}, -1];
  530. }
  531.  
  532. function getTags()
  533. {
  534. var tags = window.localStorage.userTags;
  535. if(!tags)
  536. {
  537. tags = [];
  538. }
  539. else
  540. tags = JSON.parse(tags);
  541. return tags;
  542. }