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.2
  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];
  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. pageResized();
  68.  
  69. window.setTimeout(checkHeight.bind(undefined, document.body.clientHeight), 800);
  70. }
  71. /*
  72. function addTagLinks(avatar)
  73. {
  74. var tags=getTags();
  75.  
  76. var postTable=avatar.parentNode;
  77. while(postTable.tagName != 'TABLE')
  78. postTable=postTable.parentNode;
  79. if(postTable.getAttribute('id') == 'preview_wrap_0')
  80. return;
  81. var username=postTable.getElementsByTagName('strong')[0].textContent;
  82.  
  83. var id=postTable.getAttribute('id').split('post')[1];
  84.  
  85. var a=document.createElement('a');
  86. a.setAttribute('class', 'tagLink');
  87. a.setAttribute('postId', id);
  88. var place = avatar.getBoundingClientRect();
  89. var style='position: absolute; z-index: 50000000; top: '+(place.top+window.scrollY)+'px; left: '+(place.left+window.scrollX)+'px; width: '+avatar.clientWidth+'px;';
  90. style+='text-align: center; color: blue; background: rgba(200,200,200,0.8); border-radius: 0px 0px 10px 10px;';
  91. a.setAttribute('style', style);
  92.  
  93. a.innerHTML = 'Show user tags';
  94. a.href='javascript:void(0);';
  95. a.addEventListener('click', openTags.bind(undefined, username, postTable), false);
  96. document.body.appendChild(a);
  97. a.style.display='none';
  98.  
  99. avatar.addEventListener('mouseover', mouseOver.bind(undefined, a), false);
  100. avatar.addEventListener('mouseout', mouseOut.bind(undefined, avatar, a), false);
  101. }
  102. */
  103. function pageResized()
  104. {
  105. /*var tagLinks=document.getElementsByClassName('tagLink');
  106. for(var i=0; i<tagLinks.length; i++)
  107. {
  108. var t=tagLinks[i];
  109. var id=t.getAttribute('postId');
  110. var postTable=document.getElementById('post'+id);
  111. var avatar=postTable.getElementsByClassName('avatar')[0];
  112.  
  113. var place = avatar.getBoundingClientRect();
  114. var style='position: absolute; z-index: 50000000; top: '+(place.top+window.scrollY)+'px; left: '+(place.left+window.scrollX)+'px; width: '+avatar.clientWidth+'px;';
  115. style+='text-align: center; color: blue; background: rgba(200,200,200,0.8); border-radius: 0px 0px 10px 10px;';
  116. t.setAttribute('style', style);
  117. t.style.display='none';
  118. }*/
  119.  
  120. resetTags();
  121. addTags();
  122. }
  123.  
  124. function resetTags()
  125. {
  126. var posts=document.getElementsByClassName('forum_post');
  127. for(var i=0; i<posts.length-1; i++)
  128. {
  129. var p=posts[i];
  130. var avatar=p.getElementsByClassName('avatar')[0];
  131.  
  132. var postTable=p;
  133.  
  134. if(postTable.getAttribute('id') == 'preview_wrap_0')
  135. continue;
  136. var u=postTable.getElementsByTagName('strong')[0].getElementsByTagName('a')[0];
  137. var username=u.textContent;
  138.  
  139. if(avatar)
  140. {
  141. var img=avatar.getElementsByTagName('img')[0];
  142. if(img)
  143. {
  144. img.src=img.getAttribute('originalAvatar');
  145. }
  146. }
  147. u.setAttribute('style', '');
  148. postTable.setAttribute('style', '');
  149. var id=postTable.getAttribute('id').split('post')[1];
  150. var tag=document.getElementById('tag'+id);
  151. if(tag)
  152. tag.parentNode.removeChild(tag);
  153. }
  154. var hardIgnores=document.getElementsByClassName('hardIgnoreLink');
  155. for(var i=0; i<hardIgnores.length; i++)
  156. {
  157. var h=hardIgnores[i];
  158. h.parentNode.removeChild(h);
  159. }
  160. }
  161.  
  162. function addTags()
  163. {
  164. var posts=document.getElementsByClassName('forum_post');
  165. for(var i=0; i<posts.length-1; i++)
  166. {
  167. var p=posts[i];
  168. var avatar=p.getElementsByClassName('avatar')[0];
  169.  
  170. var postTable=p;
  171.  
  172. if(postTable.getAttribute('id') == 'preview_wrap_0')
  173. continue;
  174. var u=postTable.getElementsByTagName('strong')[0].getElementsByTagName('a')[0];
  175. var username=u.textContent;
  176.  
  177. var user=getUser(username)[0];
  178. if(user.replacementAvatar && avatar)
  179. {
  180. avatar.getElementsByTagName('img')[0].src=user.replacementAvatar;
  181. }
  182. if(user.usernameColour)
  183. {
  184. var style=u.getAttribute('style');
  185. if(!style)
  186. style='';
  187. u.setAttribute('style', style+' color: '+user.usernameColour+';');
  188. }
  189. if(user.postHighlight)
  190. {
  191. var style=postTable.getAttribute('style');
  192. postTable.setAttribute('style', 'box-shadow: '+user.postHighlight+' 0 0 5px 1px !important;');
  193. }
  194. if(user.tag && user.showTag)
  195. {
  196. var div=document.createElement('div');
  197. var id=postTable.getAttribute('id').split('post')[1];
  198. div.setAttribute('id', 'tag'+id);
  199. div.innerHTML = user.tag.replace(/\n/g,'<br />');
  200. var first;
  201. if(!avatar)
  202. {
  203. avatar=postTable;
  204. first=avatar;
  205. }
  206. else
  207. first=avatar.firstElementChild;
  208. var place = postTable.getBoundingClientRect();
  209. var width=300;
  210. var left=place.left+window.scrollX-width-20;
  211. if(left<0)
  212. left=0;
  213. var style='position: absolute; z-index: 50000000; 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;';
  214. style+='font-size: large; box-shadow: inset '+(user.postHighlight ? user.postHighlight : 'black')+' 0 0 20px 0; padding: 10px;';
  215. div.setAttribute('style', style);
  216. document.body.appendChild(div);
  217. var avatarHeight=first.clientHeight;
  218. var top=place.top+window.scrollY+((avatarHeight-div.clientHeight)/2);
  219. div.style.top=top+'px';
  220. if(div.clientWidth < width)
  221. {
  222. left=place.left+window.scrollX-div.clientWidth;
  223. div.style.left=left+'px';
  224. }
  225. }
  226. if(user.softIgnore)
  227. {
  228. postTable.getElementsByTagName('tr')[1].style.display='none';
  229. }
  230. if(user.hardIgnore)
  231. {
  232. var a=document.createElement('a');
  233. var hr=document.createElement('hr');
  234. hr.setAttribute('title', username);
  235. a.appendChild(hr);
  236. a.setAttribute('class', 'hardIgnoreLink');
  237. a.href=postTable.getElementsByTagName('strong')[0].getElementsByTagName('a')[0].href;
  238. postTable.parentNode.insertBefore(a, postTable);
  239. postTable.style.display='none';
  240. }
  241. }
  242. }
  243.  
  244. function openTags(username, postTable)
  245. {
  246. var div=document.getElementById('chameleonTagsDiv');
  247. if(!div)
  248. {
  249. div=document.createElement('div');
  250. div.setAttribute('id', 'chameleonTagsDiv');
  251. document.body.appendChild(div);
  252. 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%;');
  253. }
  254. div.innerHTML = '<h2>'+username+'\'s Tags<br />';
  255.  
  256. var user=getUser(username)[0];
  257.  
  258. var input=document.createElement('input');
  259. div.appendChild(input);
  260. input.placeholder='Replacement avatar URL';
  261. input.value = user.replacementAvatar ? user.replacementAvatar : '';
  262. input.addEventListener('change', changeTags.bind(undefined, div, username, postTable, input), false);
  263.  
  264. div.appendChild(document.createElement('br'));
  265.  
  266. var input=document.createElement('input');
  267. div.appendChild(input);
  268. input.placeholder='Post highlight colour';
  269. input.value = user.postHighlight ? user.postHighlight : '';
  270. input.addEventListener('change', changeTags.bind(undefined, div, username, postTable, input), false);
  271.  
  272. div.appendChild(document.createElement('br'));
  273.  
  274. var input=document.createElement('input');
  275. div.appendChild(input);
  276. input.placeholder='Username colour';
  277. input.value = user.usernameColour ? user.usernameColour : '';
  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('textarea');
  283. input.setAttribute('id', 'tagTextarea');
  284. div.appendChild(input);
  285. input.setAttribute('style', 'text-align: center; border: none;');
  286. input.placeholder='Tag';
  287. input.value = user.tag ? user.tag : '';
  288. resize('tagTextarea');
  289. input.addEventListener('keyup', resize.bind(undefined, 'tagTextarea'), false);
  290. input.addEventListener('change', changeTags.bind(undefined, div, username, postTable, input), false);
  291.  
  292. div.appendChild(document.createElement('br'));
  293.  
  294. var a=document.createElement('a');
  295. div.appendChild(a);
  296. a.innerHTML = 'Show tag: '+(user.showTag ? 'On' : 'Off');
  297. a.href='javascript:void(0);';
  298. a.addEventListener('click', changeTags.bind(undefined, div, username, postTable, a), false);
  299.  
  300. div.appendChild(document.createElement('br'));
  301.  
  302. var a=document.createElement('a');
  303. div.appendChild(a);
  304. a.innerHTML = 'Soft ignore: '+(user.softIgnore ? 'On' : 'Off');
  305. a.href='javascript:void(0);';
  306. a.addEventListener('click', changeTags.bind(undefined, div, username, postTable, a), false);
  307.  
  308. div.appendChild(document.createElement('br'));
  309.  
  310. var a=document.createElement('a');
  311. div.appendChild(a);
  312. a.innerHTML = 'Hard ignore: '+(user.hardIgnore ? '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 = 'Save';
  321. a.href='javascript:void(0);';
  322. a.addEventListener('click', saveAndClose.bind(undefined, div, username, postTable), false);
  323. }
  324.  
  325. function changeTags(div, username, table, a)
  326. {
  327. var user=getUser(username);
  328. var index=user[1];
  329. user=user[0];
  330.  
  331. var inputs=div.getElementsByTagName('input');
  332. user.replacementAvatar = inputs[0].value;
  333. user.postHighlight = inputs[1].value;
  334. user.usernameColour = inputs[2].value;
  335.  
  336. var textareas=div.getElementsByTagName('textarea');
  337. user.tag=textareas[0].value;
  338.  
  339. var as=div.getElementsByTagName('a');
  340. if(as[0] == a)
  341. {
  342. if(a.innerHTML.indexOf('On') != -1)
  343. user.showTag=false;
  344. else
  345. user.showTag=true;
  346. }
  347. if(as[1] == a)
  348. {
  349. if(a.innerHTML.indexOf('On') != -1)
  350. user.softIgnore=false;
  351. else
  352. user.softIgnore=true;
  353. }
  354. if(as[2] == a)
  355. {
  356. if(a.innerHTML.indexOf('On') != -1)
  357. user.hardIgnore=false;
  358. else
  359. user.hardIgnore=true;
  360. }
  361.  
  362. var tags=getTags();
  363. if(index != -1)
  364. tags[index]=user;
  365. else
  366. {
  367. user.username=username;
  368. tags.push(user);
  369. }
  370. window.localStorage.userTags = JSON.stringify(tags);
  371.  
  372. openTags(username, table);
  373. }
  374.  
  375. function saveAndClose(div, username, table)
  376. {
  377. resetTags();
  378. addTags();
  379. div.parentNode.removeChild(div);
  380. }
  381.  
  382. /*
  383. function mouseOver(a)
  384. {
  385. a.style.display = 'initial';
  386. }
  387.  
  388. function mouseOut(avatar, a, event)
  389. {
  390. if(event.relatedTarget == avatar || event.relatedTarget == a)
  391. return;
  392. a.style.display = 'none';
  393. }*/
  394.  
  395. function getUser(username)
  396. {
  397. var tags=getTags();
  398. for(var i=0; i<tags.length; i++)
  399. {
  400. var t=tags[i];
  401. if(t.username === username)
  402. return [t, i];
  403. }
  404. return [{}, -1];
  405. }
  406.  
  407. function getTags()
  408. {
  409. var tags = window.localStorage.userTags;
  410. if(!tags)
  411. {
  412. tags = [];
  413. }
  414. else
  415. tags = JSON.parse(tags);
  416. return tags;
  417. }