PTH User tagging

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

目前为 2016-12-24 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name PTH User tagging
  3. // @version 0.51
  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 posts=document.getElementsByClassName('forum_post');
  129. for(var i=0; i<posts.length-1; i++)
  130. {
  131. var p=posts[i];
  132. var avatar=p.getElementsByClassName('avatar')[0];
  133.  
  134. var postTable=p;
  135.  
  136. if(postTable.getAttribute('id') == 'preview_wrap_0')
  137. continue;
  138. var u=postTable.getElementsByTagName('strong')[0].getElementsByTagName('a')[0];
  139. var username=u.textContent;
  140.  
  141. if(avatar)
  142. {
  143. var img=avatar.getElementsByTagName('img')[0];
  144. if(img)
  145. {
  146. var orig=img.getAttribute('originalAvatar');
  147. if(orig)
  148. img.src=img.getAttribute('originalAvatar');
  149. }
  150. }
  151. u.setAttribute('style', '');
  152. postTable.setAttribute('style', '');
  153. var id=postTable.getAttribute('id').split('post')[1];
  154. var tag=document.getElementById('tag'+id);
  155. if(tag)
  156. tag.parentNode.removeChild(tag);
  157. }
  158. var hardIgnores=document.getElementsByClassName('hardIgnoreLink');
  159. for(var i=0; i<hardIgnores.length; i++)
  160. {
  161. var h=hardIgnores[i];
  162. h.parentNode.removeChild(h);
  163. }
  164. }
  165.  
  166. function addTags()
  167. {
  168. var posts=document.getElementsByClassName('forum_post');
  169. for(var i=0; i<posts.length-1; i++)
  170. {
  171. var p=posts[i];
  172. var avatar=p.getElementsByClassName('avatar')[0];
  173.  
  174. var postTable=p;
  175.  
  176. if(postTable.getAttribute('id') == 'preview_wrap_0')
  177. continue;
  178. var u=postTable.getElementsByTagName('strong')[0].getElementsByTagName('a')[0];
  179. var username=u.textContent;
  180.  
  181. var user=getUser(username)[0];
  182. if(user.replacementAvatar && avatar)
  183. {
  184. avatar.getElementsByTagName('img')[0].src=user.replacementAvatar;
  185. }
  186. if(user.usernameColour)
  187. {
  188. var style=u.getAttribute('style');
  189. if(!style)
  190. style='';
  191. u.setAttribute('style', style+' color: '+user.usernameColour+';');
  192. }
  193. if(user.postHighlight)
  194. {
  195. var style=postTable.getAttribute('style');
  196. postTable.setAttribute('style', 'box-shadow: '+user.postHighlight+' 0 0 5px 1px !important;');
  197. }
  198. if(user.tag && user.showTag)
  199. {
  200. var div=document.createElement('div');
  201. var id=postTable.getAttribute('id').split('post')[1];
  202. div.setAttribute('id', 'tag'+id);
  203. div.innerHTML = user.tag.replace(/\n/g,'<br />')+' ';
  204. if(!user.showTagInHeader)
  205. {
  206. var before=document.getElementById('bar'+id).firstElementChild;
  207. before.parentNode.insertBefore(div, before);
  208. div.setAttribute('style', 'display: inline-block; margin-right: 5px;');
  209. div.setAttribute('class', 'r10');
  210. }
  211. else
  212. {
  213. var first;
  214. if(!avatar)
  215. {
  216. avatar=postTable;
  217. first=avatar;
  218. }
  219. else
  220. first=avatar.firstElementChild;
  221. var place = postTable.getBoundingClientRect();
  222. var width=300;
  223. var left=place.left+window.scrollX-width-20;
  224. if(left<0)
  225. left=0;
  226. 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;';
  227. style+='font-size: large; box-shadow: inset '+(user.postHighlight ? user.postHighlight : 'black')+' 0 0 20px 0; padding: 10px;';
  228. div.setAttribute('style', style);
  229. document.body.appendChild(div);
  230. var avatarHeight=first.clientHeight;
  231. var top=place.top+window.scrollY+((avatarHeight-div.clientHeight)/2);
  232. div.style.top=top+'px';
  233. if(div.clientWidth < width)
  234. {
  235. left=place.left+window.scrollX-div.clientWidth;
  236. if(left<0)
  237. left=0;
  238. div.style.left=left+'px';
  239. }
  240. }
  241. }
  242. if(user.softIgnore)
  243. {
  244. postTable.getElementsByTagName('tr')[1].style.display='none';
  245. }
  246. if(user.hardIgnore)
  247. {
  248. var a=document.createElement('a');
  249. var hr=document.createElement('hr');
  250. hr.setAttribute('title', username);
  251. a.appendChild(hr);
  252. a.setAttribute('class', 'hardIgnoreLink');
  253. a.href=postTable.getElementsByTagName('strong')[0].getElementsByTagName('a')[0].href;
  254. postTable.parentNode.insertBefore(a, postTable);
  255. postTable.style.display='none';
  256. }
  257. }
  258. }
  259.  
  260. function openTags(username, postTable)
  261. {
  262. var div=document.getElementById('chameleonTagsDiv');
  263. if(!div)
  264. {
  265. div=document.createElement('div');
  266. div.setAttribute('id', 'chameleonTagsDiv');
  267. document.body.appendChild(div);
  268. 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;');
  269. }
  270. div.innerHTML = '<h2>'+username+'\'s Tags<br />';
  271.  
  272. var user=getUser(username)[0];
  273.  
  274. var input=document.createElement('input');
  275. div.appendChild(input);
  276. input.placeholder='Replacement avatar URL';
  277. input.value = user.replacementAvatar ? user.replacementAvatar : '';
  278. input.addEventListener('change', changeTags.bind(undefined, div, username, postTable, input), false);
  279.  
  280. div.appendChild(document.createElement('br'));
  281.  
  282. var input=document.createElement('input');
  283. div.appendChild(input);
  284. input.placeholder='Post highlight colour';
  285. input.value = user.postHighlight ? user.postHighlight : '';
  286. input.addEventListener('change', changeTags.bind(undefined, div, username, postTable, input), false);
  287.  
  288. div.appendChild(document.createElement('br'));
  289.  
  290. var input=document.createElement('input');
  291. div.appendChild(input);
  292. input.placeholder='Username colour';
  293. input.value = user.usernameColour ? user.usernameColour : '';
  294. input.addEventListener('change', changeTags.bind(undefined, div, username, postTable, input), false);
  295.  
  296. div.appendChild(document.createElement('br'));
  297.  
  298. var input=document.createElement('textarea');
  299. input.setAttribute('id', 'tagTextarea');
  300. div.appendChild(input);
  301. input.setAttribute('style', 'text-align: center; border: none;');
  302. input.placeholder='Tag';
  303. input.value = user.tag ? user.tag : '';
  304. resize('tagTextarea');
  305. input.addEventListener('keyup', resize.bind(undefined, 'tagTextarea'), false);
  306. input.addEventListener('change', changeTags.bind(undefined, div, username, postTable, input), false);
  307.  
  308. div.appendChild(document.createElement('br'));
  309.  
  310. var a=document.createElement('a');
  311. div.appendChild(a);
  312. a.innerHTML = 'Show tag: '+(user.showTag ? 'On' : 'Off');
  313. a.href='javascript:void(0);';
  314. a.addEventListener('click', changeTags.bind(undefined, div, username, postTable, a), false);
  315.  
  316. div.appendChild(document.createElement('br'));
  317.  
  318. var a=document.createElement('a');
  319. div.appendChild(a);
  320. a.innerHTML = 'Show tag left of avatar: '+(user.showTagInHeader ? 'On' : 'Off');
  321. a.href='javascript:void(0);';
  322. a.addEventListener('click', changeTags.bind(undefined, div, username, postTable, a), false);
  323.  
  324. div.appendChild(document.createElement('br'));
  325.  
  326. var a=document.createElement('a');
  327. div.appendChild(a);
  328. a.innerHTML = 'Soft ignore: '+(user.softIgnore ? 'On' : 'Off');
  329. a.href='javascript:void(0);';
  330. a.addEventListener('click', changeTags.bind(undefined, div, username, postTable, a), false);
  331.  
  332. div.appendChild(document.createElement('br'));
  333.  
  334. var a=document.createElement('a');
  335. div.appendChild(a);
  336. a.innerHTML = 'Hard ignore: '+(user.hardIgnore ? 'On' : 'Off');
  337. a.href='javascript:void(0);';
  338. a.addEventListener('click', changeTags.bind(undefined, div, username, postTable, a), false);
  339.  
  340. div.appendChild(document.createElement('br'));
  341.  
  342. var a=document.createElement('a');
  343. div.appendChild(a);
  344. a.innerHTML = 'Save';
  345. a.href='javascript:void(0);';
  346. a.addEventListener('click', saveAndClose.bind(undefined, div, username, postTable), false);
  347. }
  348.  
  349. function changeTags(div, username, table, a)
  350. {
  351. var user=getUser(username);
  352. var index=user[1];
  353. user=user[0];
  354.  
  355. var inputs=div.getElementsByTagName('input');
  356. user.replacementAvatar = inputs[0].value;
  357. user.postHighlight = inputs[1].value;
  358. user.usernameColour = inputs[2].value;
  359.  
  360. var textareas=div.getElementsByTagName('textarea');
  361. user.tag=textareas[0].value;
  362.  
  363. var as=div.getElementsByTagName('a');
  364. if(as[0] == a)
  365. {
  366. if(a.innerHTML.indexOf('On') != -1)
  367. user.showTag=false;
  368. else
  369. user.showTag=true;
  370. }
  371. if(as[1] == a)
  372. {
  373. if(a.innerHTML.indexOf('On') != -1)
  374. user.showTagInHeader=false;
  375. else
  376. user.showTagInHeader=true;
  377. }
  378. if(as[2] == a)
  379. {
  380. if(a.innerHTML.indexOf('On') != -1)
  381. user.softIgnore=false;
  382. else
  383. user.softIgnore=true;
  384. }
  385. if(as[3] == a)
  386. {
  387. if(a.innerHTML.indexOf('On') != -1)
  388. user.hardIgnore=false;
  389. else
  390. user.hardIgnore=true;
  391. }
  392.  
  393. var tags=getTags();
  394. if(index != -1)
  395. tags[index]=user;
  396. else
  397. {
  398. user.username=username;
  399. tags.push(user);
  400. }
  401. window.localStorage.userTags = JSON.stringify(tags);
  402.  
  403. openTags(username, table);
  404. }
  405.  
  406. function saveAndClose(div, username, table)
  407. {
  408. resetTags();
  409. addTags();
  410. div.parentNode.removeChild(div);
  411. }
  412.  
  413. /*
  414. function mouseOver(a)
  415. {
  416. a.style.display = 'initial';
  417. }
  418.  
  419. function mouseOut(avatar, a, event)
  420. {
  421. if(event.relatedTarget == avatar || event.relatedTarget == a)
  422. return;
  423. a.style.display = 'none';
  424. }*/
  425.  
  426. function getUser(username)
  427. {
  428. var tags=getTags();
  429. for(var i=0; i<tags.length; i++)
  430. {
  431. var t=tags[i];
  432. if(t.username === username)
  433. return [t, i];
  434. }
  435. return [{}, -1];
  436. }
  437.  
  438. function getTags()
  439. {
  440. var tags = window.localStorage.userTags;
  441. if(!tags)
  442. {
  443. tags = [];
  444. }
  445. else
  446. tags = JSON.parse(tags);
  447. return tags;
  448. }