Greasy Fork 还支持 简体中文。

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.35
  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. var first;
  205. if(!avatar)
  206. {
  207. avatar=postTable;
  208. first=avatar;
  209. }
  210. else
  211. first=avatar.firstElementChild;
  212. var place = postTable.getBoundingClientRect();
  213. var width=300;
  214. var left=place.left+window.scrollX-width-20;
  215. if(left<0)
  216. left=0;
  217. 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;';
  218. style+='font-size: large; box-shadow: inset '+(user.postHighlight ? user.postHighlight : 'black')+' 0 0 20px 0; padding: 10px;';
  219. div.setAttribute('style', style);
  220. document.body.appendChild(div);
  221. var avatarHeight=first.clientHeight;
  222. var top=place.top+window.scrollY+((avatarHeight-div.clientHeight)/2);
  223. div.style.top=top+'px';
  224. if(div.clientWidth < width)
  225. {
  226. left=place.left+window.scrollX-div.clientWidth;
  227. if(left<0)
  228. left=0;
  229. div.style.left=left+'px';
  230. }
  231. }
  232. if(user.softIgnore)
  233. {
  234. postTable.getElementsByTagName('tr')[1].style.display='none';
  235. }
  236. if(user.hardIgnore)
  237. {
  238. var a=document.createElement('a');
  239. var hr=document.createElement('hr');
  240. hr.setAttribute('title', username);
  241. a.appendChild(hr);
  242. a.setAttribute('class', 'hardIgnoreLink');
  243. a.href=postTable.getElementsByTagName('strong')[0].getElementsByTagName('a')[0].href;
  244. postTable.parentNode.insertBefore(a, postTable);
  245. postTable.style.display='none';
  246. }
  247. }
  248. }
  249.  
  250. function openTags(username, postTable)
  251. {
  252. var div=document.getElementById('chameleonTagsDiv');
  253. if(!div)
  254. {
  255. div=document.createElement('div');
  256. div.setAttribute('id', 'chameleonTagsDiv');
  257. document.body.appendChild(div);
  258. 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%;');
  259. }
  260. div.innerHTML = '<h2>'+username+'\'s Tags<br />';
  261.  
  262. var user=getUser(username)[0];
  263.  
  264. var input=document.createElement('input');
  265. div.appendChild(input);
  266. input.placeholder='Replacement avatar URL';
  267. input.value = user.replacementAvatar ? user.replacementAvatar : '';
  268. input.addEventListener('change', changeTags.bind(undefined, div, username, postTable, input), false);
  269.  
  270. div.appendChild(document.createElement('br'));
  271.  
  272. var input=document.createElement('input');
  273. div.appendChild(input);
  274. input.placeholder='Post highlight colour';
  275. input.value = user.postHighlight ? user.postHighlight : '';
  276. input.addEventListener('change', changeTags.bind(undefined, div, username, postTable, input), false);
  277.  
  278. div.appendChild(document.createElement('br'));
  279.  
  280. var input=document.createElement('input');
  281. div.appendChild(input);
  282. input.placeholder='Username colour';
  283. input.value = user.usernameColour ? user.usernameColour : '';
  284. input.addEventListener('change', changeTags.bind(undefined, div, username, postTable, input), false);
  285.  
  286. div.appendChild(document.createElement('br'));
  287.  
  288. var input=document.createElement('textarea');
  289. input.setAttribute('id', 'tagTextarea');
  290. div.appendChild(input);
  291. input.setAttribute('style', 'text-align: center; border: none;');
  292. input.placeholder='Tag';
  293. input.value = user.tag ? user.tag : '';
  294. resize('tagTextarea');
  295. input.addEventListener('keyup', resize.bind(undefined, 'tagTextarea'), false);
  296. input.addEventListener('change', changeTags.bind(undefined, div, username, postTable, input), false);
  297.  
  298. div.appendChild(document.createElement('br'));
  299.  
  300. var a=document.createElement('a');
  301. div.appendChild(a);
  302. a.innerHTML = 'Show tag: '+(user.showTag ? 'On' : 'Off');
  303. a.href='javascript:void(0);';
  304. a.addEventListener('click', changeTags.bind(undefined, div, username, postTable, a), false);
  305.  
  306. div.appendChild(document.createElement('br'));
  307.  
  308. var a=document.createElement('a');
  309. div.appendChild(a);
  310. a.innerHTML = 'Soft ignore: '+(user.softIgnore ? 'On' : 'Off');
  311. a.href='javascript:void(0);';
  312. a.addEventListener('click', changeTags.bind(undefined, div, username, postTable, a), false);
  313.  
  314. div.appendChild(document.createElement('br'));
  315.  
  316. var a=document.createElement('a');
  317. div.appendChild(a);
  318. a.innerHTML = 'Hard ignore: '+(user.hardIgnore ? 'On' : 'Off');
  319. a.href='javascript:void(0);';
  320. a.addEventListener('click', changeTags.bind(undefined, div, username, postTable, a), false);
  321.  
  322. div.appendChild(document.createElement('br'));
  323.  
  324. var a=document.createElement('a');
  325. div.appendChild(a);
  326. a.innerHTML = 'Save';
  327. a.href='javascript:void(0);';
  328. a.addEventListener('click', saveAndClose.bind(undefined, div, username, postTable), false);
  329. }
  330.  
  331. function changeTags(div, username, table, a)
  332. {
  333. var user=getUser(username);
  334. var index=user[1];
  335. user=user[0];
  336.  
  337. var inputs=div.getElementsByTagName('input');
  338. user.replacementAvatar = inputs[0].value;
  339. user.postHighlight = inputs[1].value;
  340. user.usernameColour = inputs[2].value;
  341.  
  342. var textareas=div.getElementsByTagName('textarea');
  343. user.tag=textareas[0].value;
  344.  
  345. var as=div.getElementsByTagName('a');
  346. if(as[0] == a)
  347. {
  348. if(a.innerHTML.indexOf('On') != -1)
  349. user.showTag=false;
  350. else
  351. user.showTag=true;
  352. }
  353. if(as[1] == a)
  354. {
  355. if(a.innerHTML.indexOf('On') != -1)
  356. user.softIgnore=false;
  357. else
  358. user.softIgnore=true;
  359. }
  360. if(as[2] == a)
  361. {
  362. if(a.innerHTML.indexOf('On') != -1)
  363. user.hardIgnore=false;
  364. else
  365. user.hardIgnore=true;
  366. }
  367.  
  368. var tags=getTags();
  369. if(index != -1)
  370. tags[index]=user;
  371. else
  372. {
  373. user.username=username;
  374. tags.push(user);
  375. }
  376. window.localStorage.userTags = JSON.stringify(tags);
  377.  
  378. openTags(username, table);
  379. }
  380.  
  381. function saveAndClose(div, username, table)
  382. {
  383. resetTags();
  384. addTags();
  385. div.parentNode.removeChild(div);
  386. }
  387.  
  388. /*
  389. function mouseOver(a)
  390. {
  391. a.style.display = 'initial';
  392. }
  393.  
  394. function mouseOut(avatar, a, event)
  395. {
  396. if(event.relatedTarget == avatar || event.relatedTarget == a)
  397. return;
  398. a.style.display = 'none';
  399. }*/
  400.  
  401. function getUser(username)
  402. {
  403. var tags=getTags();
  404. for(var i=0; i<tags.length; i++)
  405. {
  406. var t=tags[i];
  407. if(t.username === username)
  408. return [t, i];
  409. }
  410. return [{}, -1];
  411. }
  412.  
  413. function getTags()
  414. {
  415. var tags = window.localStorage.userTags;
  416. if(!tags)
  417. {
  418. tags = [];
  419. }
  420. else
  421. tags = JSON.parse(tags);
  422. return tags;
  423. }