PTH User tagging

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

当前为 2018-04-08 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name PTH User tagging
  3. // @version 1.0
  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. console.log(username);
  139. }
  140. if(user.customTitle)
  141. {
  142. document.getElementsByClassName('user_title')[0].innerHTML='('+user.customTitle+')';
  143. }
  144. }
  145.  
  146.  
  147. function checkHeight(height)
  148. {
  149. if(height != document.body.clientHeight)
  150. {
  151. pageResized();
  152. }
  153.  
  154. window.setTimeout(checkHeight.bind(undefined, document.body.clientHeight), 800);
  155. }
  156. /*
  157. function addTagLinks(avatar)
  158. {
  159. var tags=getTags();
  160.  
  161. var postTable=avatar.parentNode;
  162. while(postTable.tagName != 'TABLE')
  163. postTable=postTable.parentNode;
  164. if(postTable.getAttribute('id') == 'preview_wrap_0')
  165. return;
  166. var username=postTable.getElementsByTagName('strong')[0].textContent;
  167.  
  168. var id=postTable.getAttribute('id').split('post')[1];
  169.  
  170. var a=document.createElement('a');
  171. a.setAttribute('class', 'tagLink');
  172. a.setAttribute('postId', id);
  173. var place = avatar.getBoundingClientRect();
  174. var style='position: absolute; z-index: 50000000; top: '+(place.top+window.scrollY)+'px; left: '+(place.left+window.scrollX)+'px; width: '+avatar.clientWidth+'px;';
  175. style+='text-align: center; color: blue; background: rgba(200,200,200,0.8); border-radius: 0px 0px 10px 10px;';
  176. a.setAttribute('style', style);
  177.  
  178. a.innerHTML = 'Show user tags';
  179. a.href='javascript:void(0);';
  180. a.addEventListener('click', openTags.bind(undefined, username, postTable), false);
  181. document.body.appendChild(a);
  182. a.style.display='none';
  183.  
  184. avatar.addEventListener('mouseover', mouseOver.bind(undefined, a), false);
  185. avatar.addEventListener('mouseout', mouseOut.bind(undefined, avatar, a), false);
  186. }
  187. */
  188. function pageResized()
  189. {
  190. /*var tagLinks=document.getElementsByClassName('tagLink');
  191. for(var i=0; i<tagLinks.length; i++)
  192. {
  193. var t=tagLinks[i];
  194. var id=t.getAttribute('postId');
  195. var postTable=document.getElementById('post'+id);
  196. var avatar=postTable.getElementsByClassName('avatar')[0];
  197.  
  198. var place = avatar.getBoundingClientRect();
  199. var style='position: absolute; z-index: 50000000; top: '+(place.top+window.scrollY)+'px; left: '+(place.left+window.scrollX)+'px; width: '+avatar.clientWidth+'px;';
  200. style+='text-align: center; color: blue; background: rgba(200,200,200,0.8); border-radius: 0px 0px 10px 10px;';
  201. t.setAttribute('style', style);
  202. t.style.display='none';
  203. }*/
  204.  
  205. resetTags();
  206. addTags();
  207. }
  208.  
  209. function resetTags()
  210. {
  211. var ignoredQuotes=document.getElementsByClassName('toggleQuote');
  212. for(var i=0; i<ignoredQuotes.length; i++)
  213. {
  214. var ig=ignoredQuotes[i];
  215. ig.nextElementSibling.style.display='';
  216. ig.parentNode.removeChild(ig);
  217. }
  218.  
  219. var posts=document.getElementsByClassName('forum_post');
  220. var length;
  221. if(current_site==="RED")
  222. length=posts.length-1;
  223. else if(current_site==="PTP")
  224. length=posts.length;
  225. for(var i=0; i<length; i++)
  226. {
  227. var p=posts[i];
  228. var avatar;
  229. if(current_site==="RED")
  230. avatar=p.getElementsByClassName('avatar')[0];
  231. else if(current_site==="PTP")
  232. avatar=p.getElementsByClassName('forum-post__avatar')[0];
  233.  
  234. var postTable=p;
  235.  
  236. if(postTable.getAttribute('id') == 'preview_wrap_0')
  237. continue;
  238. var u=postTable.getElementsByTagName('strong')[0].getElementsByTagName('a')[0];
  239. var username=u.textContent;
  240.  
  241.  
  242. var c=postTable.getElementsByClassName('user_title');
  243. if(c.length > 0)
  244. {
  245. var orig=c[0].getAttribute('original');
  246. if(orig)
  247. c[0].innerHTML=orig;
  248. }
  249.  
  250. if(avatar)
  251. {
  252. var img=avatar.getElementsByTagName('img')[0];
  253. if(img)
  254. {
  255. var orig=img.getAttribute('originalAvatar');
  256. if(orig)
  257. img.src=img.getAttribute('originalAvatar');
  258. }
  259. }
  260. u.setAttribute('style', '');
  261. postTable.setAttribute('style', '');
  262. var tr;
  263. if(current_site==="RED")
  264. tr=postTable.getElementsByTagName('tr')[1];
  265. else if(current_site==="PTP")
  266. tr=postTable.getElementsByClassName('forum-post__avatar-and-body')[0];
  267. if(tr.getAttribute('stayHidden')!=="true")
  268. {
  269. tr.style.display='';
  270. }
  271. var id=postTable.getAttribute('id').split('post')[1];
  272. var tag=document.getElementById('tag'+id);
  273. if(tag)
  274. tag.parentNode.removeChild(tag);
  275. }
  276. var hardIgnores=document.getElementsByClassName('hardIgnoreLink');
  277. for(var i=0; i<hardIgnores.length; i++)
  278. {
  279. var h=hardIgnores[i];
  280. h.parentNode.removeChild(h);
  281. }
  282. }
  283.  
  284. function addTags()
  285. {
  286. var quotes=document.getElementsByTagName('blockquote');
  287. for(var i=0; i<quotes.length; i++)
  288. {
  289. var q=quotes[i];
  290. var username = q.previousElementSibling;
  291. if(username)
  292. {
  293. username=username.textContent.split(' ')[0];
  294. var user=getUser(username)[0];
  295. if(user.softIgnore || user.hardIgnore)
  296. {
  297. var a=document.createElement('a');
  298. a.href='javascript:void(0);';
  299. a.textContent='<Ignored>';
  300. a.addEventListener('click', toggleQuote.bind(undefined, a, q), false);
  301. a.setAttribute('class', 'toggleQuote');
  302. q.parentNode.insertBefore(a, q);
  303. if(q.getAttribute('unIgnored')=="true")
  304. {
  305. a.textContent='Ignore';
  306. }
  307. else
  308. q.style.display='none';
  309. }
  310. }
  311. }
  312.  
  313. var posts=document.getElementsByClassName('forum_post');
  314. var length;
  315. if(current_site==="RED")
  316. length=posts.length-1;
  317. else if(current_site==="PTP")
  318. length=posts.length;
  319. for(var i=0; i<length; i++)
  320. {
  321. var p=posts[i];
  322. var avatar;
  323. if(current_site==="RED")
  324. avatar=p.getElementsByClassName('avatar')[0];
  325. else if(current_site==="PTP")
  326. avatar=p.getElementsByClassName('forum-post__avatar')[0];
  327.  
  328. var postTable=p;
  329.  
  330. if(postTable.getAttribute('id') == 'preview_wrap_0')
  331. continue;
  332. var u=postTable.getElementsByTagName('strong')[0].getElementsByTagName('a')[0];
  333. var username=u.textContent;
  334.  
  335. var user=getUser(username)[0];
  336. if(user.replacementAvatar && avatar)
  337. {
  338. avatar.getElementsByTagName('img')[0].src=user.replacementAvatar;
  339. }
  340. if(user.usernameColour)
  341. {
  342. var style=u.getAttribute('style');
  343. if(!style)
  344. style='';
  345. u.setAttribute('style', style+' color: '+user.usernameColour+';');
  346. }
  347. if(user.postHighlight)
  348. {
  349. var style=postTable.getAttribute('style');
  350. postTable.setAttribute('style', 'box-shadow: '+user.postHighlight+' 0 0 5px 1px !important;');
  351. }
  352. if(user.customTitle)
  353. {
  354. var c=postTable.getElementsByClassName('user_title');
  355. if(c.length > 0)
  356. c=c[0];
  357. else
  358. {
  359. c=document.createElement('span');
  360. c.setAttribute('class', 'user_title');
  361. var before=postTable.getElementsByClassName('time')[0];
  362. before.parentNode.insertBefore(c, before);
  363. }
  364. if(!c.getAttribute('original'))
  365. c.setAttribute('original', c.innerHTML);
  366.  
  367. c.innerHTML='('+user.customTitle+')';
  368. }
  369. if(user.tag && user.showTag)
  370. {
  371. var div=document.createElement('div');
  372. var id=postTable.getAttribute('id').split('post')[1];
  373. div.setAttribute('id', 'tag'+id);
  374. div.innerHTML = user.tag.replace(/\n/g,'<br />')+' ';
  375. if(!user.showTagInHeader)
  376. {
  377. var before=document.getElementById('bar'+id).firstElementChild;
  378. before.parentNode.insertBefore(div, before);
  379. div.setAttribute('style', 'display: inline-block; margin-right: 5px;');
  380. div.setAttribute('class', 'r10');
  381. }
  382. else
  383. {
  384. var first;
  385. if(!avatar)
  386. {
  387. avatar=postTable;
  388. first=avatar;
  389. }
  390. else
  391. first=avatar.firstElementChild;
  392. var place = postTable.getBoundingClientRect();
  393. var width=300;
  394. var left=place.left+window.scrollX-width-20;
  395. if(left<0)
  396. left=0;
  397. 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;';
  398. style+='font-size: large; box-shadow: inset '+(user.postHighlight ? user.postHighlight : 'black')+' 0 0 20px 0; padding: 10px;';
  399. div.setAttribute('style', style);
  400. document.body.appendChild(div);
  401. var avatarHeight=first.clientHeight;
  402. var top=place.top+window.scrollY+((avatarHeight-div.clientHeight)/2);
  403. div.style.top=top+'px';
  404. if(div.clientWidth < width)
  405. {
  406. left=place.left+window.scrollX-div.clientWidth;
  407. if(left<0)
  408. left=0;
  409. div.style.left=left+'px';
  410. }
  411. }
  412. }
  413. if(user.softIgnore)
  414. {
  415.  
  416. var tr;
  417. if(current_site==="RED")
  418. tr=postTable.getElementsByTagName('tr')[1];
  419. else if(current_site==="PTP")
  420. tr=postTable.getElementsByClassName('forum-post__avatar-and-body')[0];
  421. tr.style.display='none';
  422. }
  423. if(user.hardIgnore)
  424. {
  425. var a=document.createElement('a');
  426. var hr=document.createElement('hr');
  427. hr.setAttribute('title', username);
  428. a.appendChild(hr);
  429. a.setAttribute('class', 'hardIgnoreLink');
  430. a.href=postTable.getElementsByTagName('strong')[0].getElementsByTagName('a')[0].href;
  431. postTable.parentNode.insertBefore(a, postTable);
  432. postTable.style.display='none';
  433. }
  434. }
  435. }
  436.  
  437. function toggleQuote(a, q)
  438. {
  439. if(a.innerHTML.indexOf('Ignored') != -1)
  440. {
  441. a.textContent = 'Ignore';
  442. q.style.display='';
  443. q.setAttribute('unIgnored', "true");
  444. }
  445. else
  446. {
  447. a.textContent = '<Ignored>';
  448. q.style.display='none';
  449. q.setAttribute('unIgnored', "false");
  450. }
  451. }
  452.  
  453. function openTags(username, postTable)
  454. {
  455. var div=document.getElementById('chameleonTagsDiv');
  456. if(!div)
  457. {
  458. div=document.createElement('div');
  459. div.setAttribute('id', 'chameleonTagsDiv');
  460. document.body.appendChild(div);
  461. 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;');
  462. }
  463. div.innerHTML = '<h2>'+username+'\'s Tags<br />';
  464.  
  465. var user=getUser(username)[0];
  466.  
  467. var input=document.createElement('input');
  468. div.appendChild(input);
  469. input.placeholder='Replacement avatar URL';
  470. input.value = user.replacementAvatar ? user.replacementAvatar : '';
  471. input.addEventListener('change', changeTags.bind(undefined, div, username, postTable, input), false);
  472.  
  473. div.appendChild(document.createElement('br'));
  474.  
  475. var input=document.createElement('input');
  476. div.appendChild(input);
  477. input.placeholder='Replacement custom title';
  478. input.value = user.customTitle ? user.customTitle : '';
  479. input.addEventListener('change', changeTags.bind(undefined, div, username, postTable, input), false);
  480.  
  481. div.appendChild(document.createElement('br'));
  482.  
  483. var input=document.createElement('input');
  484. div.appendChild(input);
  485. input.placeholder='Post highlight colour';
  486. input.value = user.postHighlight ? user.postHighlight : '';
  487. input.addEventListener('change', changeTags.bind(undefined, div, username, postTable, input), false);
  488.  
  489. div.appendChild(document.createElement('br'));
  490.  
  491. var input=document.createElement('input');
  492. div.appendChild(input);
  493. input.placeholder='Username colour';
  494. input.value = user.usernameColour ? user.usernameColour : '';
  495. input.addEventListener('change', changeTags.bind(undefined, div, username, postTable, input), false);
  496.  
  497. div.appendChild(document.createElement('br'));
  498.  
  499. var input=document.createElement('textarea');
  500. input.setAttribute('id', 'tagTextarea');
  501. div.appendChild(input);
  502. input.setAttribute('style', 'text-align: center; border: none;');
  503. input.placeholder='Tag';
  504. input.value = user.tag ? user.tag : '';
  505. resize('tagTextarea');
  506. input.addEventListener('keyup', resize.bind(undefined, 'tagTextarea'), false);
  507. input.addEventListener('change', changeTags.bind(undefined, div, username, postTable, input), false);
  508.  
  509. div.appendChild(document.createElement('br'));
  510.  
  511. var a=document.createElement('a');
  512. div.appendChild(a);
  513. a.innerHTML = 'Show tag: '+(user.showTag ? 'On' : 'Off');
  514. a.href='javascript:void(0);';
  515. a.addEventListener('click', changeTags.bind(undefined, div, username, postTable, a), false);
  516.  
  517. div.appendChild(document.createElement('br'));
  518.  
  519. var a=document.createElement('a');
  520. div.appendChild(a);
  521. a.innerHTML = 'Show tag left of avatar: '+(user.showTagInHeader ? 'On' : 'Off');
  522. a.href='javascript:void(0);';
  523. a.addEventListener('click', changeTags.bind(undefined, div, username, postTable, a), false);
  524.  
  525. div.appendChild(document.createElement('br'));
  526.  
  527. var a=document.createElement('a');
  528. div.appendChild(a);
  529. a.innerHTML = 'Soft ignore: '+(user.softIgnore ? 'On' : 'Off');
  530. a.href='javascript:void(0);';
  531. a.addEventListener('click', changeTags.bind(undefined, div, username, postTable, a), false);
  532.  
  533. div.appendChild(document.createElement('br'));
  534.  
  535. var a=document.createElement('a');
  536. div.appendChild(a);
  537. a.innerHTML = 'Hard ignore: '+(user.hardIgnore ? 'On' : 'Off');
  538. a.href='javascript:void(0);';
  539. a.addEventListener('click', changeTags.bind(undefined, div, username, postTable, a), false);
  540.  
  541. div.appendChild(document.createElement('br'));
  542.  
  543. var a=document.createElement('a');
  544. div.appendChild(a);
  545. a.innerHTML = 'Save';
  546. a.href='javascript:void(0);';
  547. a.addEventListener('click', saveAndClose.bind(undefined, div, username, postTable), false);
  548. }
  549.  
  550. function changeTags(div, username, table, a)
  551. {
  552. var user=getUser(username);
  553. var index=user[1];
  554. user=user[0];
  555.  
  556. var inputs=div.getElementsByTagName('input');
  557. user.replacementAvatar = inputs[0].value;
  558. user.customTitle = inputs[1].value;
  559. user.postHighlight = inputs[2].value;
  560. user.usernameColour = inputs[3].value;
  561.  
  562. var textareas=div.getElementsByTagName('textarea');
  563. user.tag=textareas[0].value;
  564.  
  565. var as=div.getElementsByTagName('a');
  566. if(as[0] == a)
  567. {
  568. if(a.innerHTML.indexOf('On') != -1)
  569. user.showTag=false;
  570. else
  571. user.showTag=true;
  572. }
  573. if(as[1] == a)
  574. {
  575. if(a.innerHTML.indexOf('On') != -1)
  576. user.showTagInHeader=false;
  577. else
  578. user.showTagInHeader=true;
  579. }
  580. if(as[2] == a)
  581. {
  582. if(a.innerHTML.indexOf('On') != -1)
  583. user.softIgnore=false;
  584. else
  585. user.softIgnore=true;
  586. }
  587. if(as[3] == a)
  588. {
  589. if(a.innerHTML.indexOf('On') != -1)
  590. user.hardIgnore=false;
  591. else
  592. user.hardIgnore=true;
  593. }
  594.  
  595. var tags=getTags();
  596. if(index != -1)
  597. tags[index]=user;
  598. else
  599. {
  600. user.username=username;
  601. tags.push(user);
  602. }
  603. window.localStorage.userTags = JSON.stringify(tags);
  604.  
  605. openTags(username, table);
  606. }
  607.  
  608. function saveAndClose(div, username, table)
  609. {
  610. resetTags();
  611. addTags();
  612. setProfile();
  613. div.parentNode.removeChild(div);
  614. }
  615.  
  616. /*
  617. function mouseOver(a)
  618. {
  619. a.style.display = 'initial';
  620. }
  621.  
  622. function mouseOut(avatar, a, event)
  623. {
  624. if(event.relatedTarget == avatar || event.relatedTarget == a)
  625. return;
  626. a.style.display = 'none';
  627. }*/
  628.  
  629. function getUser(username)
  630. {
  631. var tags=getTags();
  632. for(var i=0; i<tags.length; i++)
  633. {
  634. var t=tags[i];
  635. if(t.username === username)
  636. return [t, i];
  637. }
  638. return [{}, -1];
  639. }
  640.  
  641. function getTags()
  642. {
  643. var tags = window.localStorage.userTags;
  644. if(!tags)
  645. {
  646. tags = [];
  647. }
  648. else
  649. tags = JSON.parse(tags);
  650. return tags;
  651. }