PTH User tagging

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

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

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