PTH User tagging

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

目前為 2018-04-19 提交的版本,檢視 最新版本

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