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