Greasy Fork 支持简体中文。

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