PTH User tagging

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

目前为 2016-12-23 提交的版本,查看 最新版本

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