TPC Sticky User Toolbar

Transforms "User Options" on the left sidebar into a toolbar that sticks to the top

目前为 2016-03-19 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name TPC Sticky User Toolbar
  3. // @namespace com.tipidpc.velc2009
  4. // @description Transforms "User Options" on the left sidebar into a toolbar that sticks to the top
  5. // @version 1.0.0
  6. // @match https://tipidpc.com/*
  7. // @license GPL-3.0
  8. // @grant GM_addStyle
  9. // ==/UserScript==
  10.  
  11. var userOptions = document.getElementById('user-options');
  12. if (!userOptions) return;
  13. userOptions.parentElement.removeChild(userOptions);
  14.  
  15. var userOptionsList = userOptions.getElementsByTagName('ul')[0];
  16.  
  17. userOptionsList.id = "user-options-list";
  18. GM_addStyle('#user-options-list {background-color:#690; padding:5px; top:0; box-sizing:border-box;}');
  19. GM_addStyle('#user-options-list {border-bottom:1px solid #333;}');
  20. GM_addStyle('#user-options-list li {background-image:none; display:inline; margin-bottom:0;}');
  21. GM_addStyle('#user-options-list li:last-of-type {float:right; margin-right:10px;}');
  22. GM_addStyle('#user-options-list li a {color: #fff;}');
  23.  
  24. var container = document.getElementById('container');
  25. var header = document.getElementById('header');
  26.  
  27. var anchor = document.createElement('div');
  28. header.appendChild(anchor);
  29. header.appendChild(userOptionsList);
  30.  
  31. var icon = (function() {
  32. var icon = document.createElement('img');
  33. icon.src = 'favicon.ico';
  34. icon.style.verticalAlign = 'middle';
  35. var link = document.createElement('a');
  36. link.href = '/index.php';
  37. link.style.marginLeft = '10px';
  38. link.appendChild(icon);
  39. return link;
  40. })();
  41.  
  42. function updateUserOptionsListPosition() {
  43. if (document.body.scrollTop > anchor.offsetTop) {
  44. if (userOptionsList.style.position != 'fixed') {
  45. userOptionsList.style.position = 'fixed';
  46. anchor.style.height = userOptionsList.offsetHeight + 'px';
  47. // userOptionsList.insertBefore(icon, userOptionsList.children[0]);
  48. }
  49. } else {
  50. if (userOptionsList.style.position != 'static') {
  51. userOptionsList.style.position = 'static';
  52. anchor.style.height = '0px';
  53. // userOptionsList.removeChild(icon);
  54. }
  55. }
  56. }
  57.  
  58. function updateUserOptionsListWidth() {
  59. userOptionsList.style.width = container.offsetWidth + 'px';
  60. }
  61.  
  62. if (window.addEventListener) {
  63. window.addEventListener('scroll', updateUserOptionsListPosition);
  64. window.addEventListener('resize', updateUserOptionsListWidth);
  65. } else {
  66. window.attachEvent('scroll', updateUserOptionsListPosition);
  67. window.attachEvent('resize', updateUserOptionsListWidth);
  68. }
  69.  
  70. updateUserOptionsListPosition();
  71. updateUserOptionsListWidth();