Kaskus - Scroll to Top

Add button on Kaskus topbar to quickly scroll to top.

当前为 2014-02-19 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @id kaskus-scroll-to-top@loucypher
  3. // @name Kaskus - Scroll to Top
  4. // @namespace https://userscripts.org/users/12
  5. // @description Add button on Kaskus topbar to quickly scroll to top.
  6. // @version 1.20140219123728
  7. // @author LouCypher
  8. // @license WTFPL
  9. // @screenshot https://mediacru.sh/Qjrp3WGaeeWD.png
  10. // @icon http://loucypher.github.io/userscripts/kaskus/kaskus-48.png
  11. // @icon64URL http://loucypher.github.io/userscripts/kaskus/kaskus-64.png
  12. // @contributionURL http://loucypher.github.io/userscripts/donate.html?Kaskus+-+Scroll+to+Top
  13. // @homepageURL https://userscripts.org/scripts/show/356658
  14. // @supportURL https://userscripts.org/scripts/discuss/356658
  15. // @resource CHANGELOG https://raw.github.com/LouCypher/userscripts/master/kaskus/scroll-to-top/CHANGELOG.txt
  16. // @resource LICENSE https://raw.github.com/LouCypher/userscripts/master/licenses/WTFPL/LICENSE.txt
  17. // @include http://www.kaskus.co.id/*
  18. // @grant unsafeWindow
  19. // @grant GM_addStyle
  20. // ==/UserScript==
  21. /* This program is free software. It comes without any warranty, to
  22. * the extent permitted by applicable law. You can redistribute it
  23. * and/or modify it under the terms of the Do What The Fuck You Want
  24. * To Public License, Version 2, as published by Sam Hocevar. See
  25. * http://www.wtfpl.net/ for more details. */
  26.  
  27.  
  28.  
  29. function $(aSelector, aNode) {
  30. return (aNode || document).querySelector(aSelector);
  31. }
  32.  
  33. function scrollToTop(aEvent) {
  34. aEvent.preventDefault();
  35. if (pageYOffset >= 1)
  36. unsafeWindow.jQuery("html").animate({scrollTop: 0}, "fast");
  37. }
  38.  
  39. function showButton() {
  40. if (pageYOffset >= header.offsetHeight)
  41. $("#scroll-to-top").className = "";
  42. else
  43. $("#scroll-to-top").className = "hide";
  44. }
  45.  
  46. var css = ".rotate-right::before{-moz-transform:rotate(90deg);"
  47. + "-webkit-transform:rotate(90deg);transform:rotate(90deg);font-size:15px;}"
  48. + "#scroll-to-top{text-align:center;width:25px;top:4px;"
  49. + (typeof opera === "object"
  50. // Opera
  51. ? "position:fixed;right:1em;background:#1484ce;"
  52. // other browsers that support CSS calc() function
  53. : "position:absolute;left:-moz-calc(50% - 12.5px);" +
  54. "left:-webkit-calc(50% - 12.5px);left:calc(50% - 12.5px);")
  55. + "}";
  56.  
  57. var header = $("#site-header .meta-header");
  58. var row = $(".row", header);
  59. if (row) {
  60. GM_addStyle(css);
  61. var link = row.insertBefore(document.createElement("a"), row.firstChild);
  62. link.id = "scroll-to-top";
  63. link.className = "hide";
  64. link.href = "#";
  65. link.rel = "tooltip";
  66. link.dataset.title = "Scroll to Top";
  67. link.dataset.placement = "bottom";
  68. link.innerHTML = '<i class="icon-step-backward header-icon rotate-right"></i>';
  69. link.addEventListener("click", scrollToTop);
  70. window.addEventListener("scroll", showButton);
  71. showButton();
  72. }