Smoothscroll

Smooth scrolling on pages using javascript and jquery

目前為 2016-10-06 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Smoothscroll
  3. // @include http*
  4. // @author Creec Winceptor
  5. // @description Smooth scrolling on pages using javascript and jquery
  6. // @namespace https://greasyfork.org/users/3167
  7. // @run-at document-idle
  8. // @grant none
  9. // @grant GM_getValue
  10. // @grant GM_setValue
  11. // @grant GM_registerMenuCommand
  12. // @version 1.1.0
  13. // ==/UserScript==
  14.  
  15. if (window.top != window.self) //don't run on frames or iframes
  16. return;
  17.  
  18.  
  19.  
  20.  
  21.  
  22. //DEFAULT SETTINGS HERE
  23. //DO NOT CHANGE ANYTHING HERE ANYMORE, USE SCRIPT COMMANDS -> CONFIGURE SMOOTHSCROLL
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30. //Smoothness factor value (how strong the smoothing effect is)
  31. //values: 1-(infinite)
  32. var smoothness = 30;
  33.  
  34. //Scroll sensitivity
  35. //values: anything? (default 1.00)
  36. var sensitivity = 1;
  37.  
  38. //Acceleration sensitivity
  39. //values: anything? (default 1.50)
  40. var acceleration = 1;
  41.  
  42. //Refreshrate setting
  43. //values: 30-144 (default = 60/72/120/144 = same as your monitor hz)
  44. var baserefreshrate = 60;
  45.  
  46.  
  47. //Alternative scrolling multiplier
  48. //values: true/false (try to set this to true if scrolling is too slow/doesn't work)
  49. var alternative_sensitivity_multiplier = false;
  50.  
  51.  
  52. //CODE STARTS HERE
  53.  
  54. var minimal_jquery_version = 200;
  55.  
  56. //max retries
  57. var jquery_retry_max = 5;
  58. var jquery_retry_count = 0;
  59.  
  60.  
  61. var DEBUG = false;
  62.  
  63. var WEBKIT = false;
  64.  
  65. //this.$ = this.jQuery = jQuery.noConflict(true);
  66.  
  67. console.log("Loading smoothscroll...");
  68.  
  69. if (smoothness>100)
  70. {
  71. smoothness = 100;
  72. }
  73.  
  74. if (baserefreshrate <= 30 || baserefreshrate>144)
  75. {
  76. baserefreshrate = 144;
  77. }
  78. refreshrate = baserefreshrate;
  79.  
  80. var animationduration = Math.round(1000/refreshrate);
  81. //var relativeratio = Math.round(51-smoothness/2)/100;
  82. var relativeratio = Math.round(1/(1+smoothness*refreshrate/baserefreshrate)*100)/100;
  83. //var relativeratio = relativeratio;
  84.  
  85.  
  86.  
  87. var lastLoop = new Date;
  88. //var lastrefreshrate = 0;
  89. function gameLoop() {
  90. var thisLoop = new Date;
  91. var refreshrate0 = 1000 / (thisLoop - lastLoop + 1);
  92. lastLoop = thisLoop;
  93. refreshrate = refreshrate + (refreshrate0-refreshrate)*0.1;
  94. refreshrate = Math.round(refreshrate);
  95. if (DEBUG)
  96. {
  97. console.log(refreshrate);
  98. }
  99. //console.log(refreshrate);
  100. animationduration = Math.round(1000/(refreshrate));
  101. //var relativeratio = Math.round(51-smoothness/2)/100;
  102. relativeratio = Math.round(1/(1+smoothness*refreshrate/baserefreshrate)*100)/100;
  103. }
  104. gameLoop();
  105.  
  106.  
  107. function InitSmoothscroll()
  108. {
  109. //LoadConfig();
  110.  
  111. //InitConfigmenu();
  112.  
  113. var startposition = false;
  114. var targetposition = 0;
  115. var position = 0;
  116.  
  117. var scrollfocus = ss$('body');
  118. var mousemoved = true;
  119. function hasScrollBarVisible(element)
  120. {
  121. //return (document.documentElement.scrollHeight !== document.documentElement.clientHeight);
  122. // Get the computed style of the body element
  123. var cStyle = element.currentStyle||window.getComputedStyle(element, "");
  124. // Check the overflow and overflowY properties for "auto" and "visible" values
  125. var scrollbar1 = cStyle.overflow == "scroll" || cStyle.overflowY == "scroll";
  126.  
  127. var scrollbar2 = cStyle.overflow == "auto" || cStyle.overflowY == "auto";
  128. var scrollbar = scrollbar1 || scrollbar2;
  129. return scrollbar;
  130. }
  131.  
  132.  
  133. function hasscrollbars(scrollfocus)
  134. {
  135. var hasvisiblescrollbars = hasScrollBarVisible(scrollfocus);
  136. var parentelement = ss$(scrollfocus).parent();
  137. if ( ss$(parentelement))
  138. {
  139. if (ss$(parentelement).is("textarea") || ss$(scrollfocus).is("textarea") || ss$(parentelement).is("article") || ss$(parentelement).is("article"))
  140. {
  141. return true;
  142. }
  143. else
  144. {
  145. if (ss$(parentelement).hasClass( "yt-scrollbar" ) || ss$(scrollfocus).hasClass( "yt-scrollbar" ))
  146. {
  147. return true;
  148. }
  149. return hasvisiblescrollbars;
  150. }
  151. }
  152. else
  153. {
  154. return hasvisiblescrollbars;
  155. }
  156. return false;
  157. }
  158.  
  159. function UpdatePosition(element)
  160. {
  161. gameLoop();
  162. var positiondelta = ss$(element)[0].getAttribute( "positiondelta" );
  163. //var smoothdelta = Math.sqrt(positiondelta*positiondelta*relativeratio);
  164. var smoothdelta = Math.sqrt(positiondelta*positiondelta*relativeratio);
  165. if (positiondelta<0)
  166. {
  167. smoothdelta = smoothdelta*(-1);
  168. }
  169. //var relative = position - ss$(element).scrollTop();
  170. //console.log("smoothdelta:" + smoothdelta);
  171. if (Math.abs( (positiondelta-smoothdelta)) <= 1 )
  172. {
  173. ss$(element).stop();
  174. ss$(element).animate({
  175. scrollTop: '+=' + Math.round(positiondelta)
  176. }, animationduration, "linear", function() {
  177. ss$(element).attr( "positiondelta",0 );
  178. ss$(element)[0].setAttribute( "positiondelta",0 );
  179. if (DEBUG)
  180. {
  181. ss$(element).css( "border", "1px solid red" );
  182. }
  183. });
  184. }
  185. else
  186. {
  187. ss$(element).stop();
  188. ss$(element).animate({
  189. scrollTop: '+=' + Math.round(smoothdelta)
  190. }, animationduration, "linear", function() {
  191. ss$(element).attr( "positiondelta",positiondelta-smoothdelta );
  192. ss$(element)[0].setAttribute("positiondelta",positiondelta-smoothdelta );
  193. UpdatePosition(element);
  194. if (DEBUG)
  195. {
  196. ss$(element).css( "border", "1px solid red" );
  197. }
  198. });
  199. }
  200. }
  201.  
  202.  
  203. function MouseScroll (e) {
  204. gameLoop();
  205. var mul = 1;
  206. if (!WEBKIT || alternative_sensitivity_multiplier)
  207. mul = 40;
  208. var x = e.deltaX*mul;
  209. var y = e.deltaY*mul;
  210. if (mousemoved)
  211. {
  212. scrollfocus = UpdateFocus(x,y);
  213. mousemoved = false;
  214. }
  215. //
  216. var positiondelta = 0;
  217. var lastscrolltop = 0;
  218.  
  219. var parentelement = ss$(scrollfocus).parent();
  220. var rolled = y;
  221. var lastscrolltop = ss$(scrollfocus).scrollTop();
  222. ss$(scrollfocus).scrollTop(lastscrolltop+rolled);
  223. if (ss$(scrollfocus).scrollTop()==lastscrolltop)
  224. {
  225. if (!ss$(scrollfocus).is("html"))
  226. {
  227. //scrollfocus = ss$(scrollfocus);
  228. //focus = UpdateFocus(event);
  229. //return MouseScroll (event);
  230. //console.log("false");
  231. //return false;
  232. scrollfocus = UpdateFocus(x,y);
  233. return false;
  234. }
  235. else
  236. {
  237. //console.log("true");
  238. //return false;
  239. }
  240. }
  241. else
  242. {
  243. e.preventDefault();
  244. }
  245. ss$(scrollfocus).scrollTop(lastscrolltop);
  246. if (ss$(scrollfocus)[0].getAttribute("positiondelta")==undefined)
  247. {
  248. positiondelta = 0;
  249. //console.log("positiondelta: undefined");
  250. }
  251. else
  252. {
  253. positiondelta = ss$(scrollfocus)[0].getAttribute("positiondelta");
  254. //console.log("positiondelta: " + positiondelta);
  255. }
  256. positiondelta = positiondelta*1;
  257. var direction = rolled/Math.abs(rolled);
  258. //var positiondeltadelta = rolled*sensitivity + Math.sqrt(Math.abs(positiondelta/rolled))*acceleration*rolled;
  259. //var positiondeltadelta = rolled*(sensitivity+Math.sqrt(Math.abs(positiondelta))*acceleration);
  260. var positiondeltadelta = Math.round(rolled*sensitivity + Math.sqrt(Math.abs(positiondelta-rolled*sensitivity))*acceleration*rolled*0.05/sensitivity);
  261. positiondelta = positiondelta + positiondeltadelta;
  262. ss$(scrollfocus)[0].setAttribute("positiondelta",positiondelta );
  263. UpdatePosition(ss$(scrollfocus));
  264. return true;
  265. }
  266.  
  267. function canscroll(element, dir)
  268. {
  269. var scrollable = ss$(element);
  270. var lastscrolltop = ss$(scrollable).scrollTop();
  271. ss$(scrollable).scrollTop(lastscrolltop+dir);
  272. if (ss$(scrollable).scrollTop()==lastscrolltop && dir!=0)
  273. {
  274. ss$(scrollable).scrollTop(lastscrolltop);
  275. return false;
  276. }
  277. else
  278. {
  279. ss$(scrollable).scrollTop(lastscrolltop);
  280. return true;
  281. }
  282. }
  283.  
  284. function UpdateFocus(x,y) {
  285. var dir = y;
  286. var nodelist = document.querySelectorAll( ":hover" );
  287. ss$(nodelist).stop();
  288. //console.log(nodelist);
  289. if (WEBKIT)
  290. {
  291. scrollfocus = ss$('body');
  292. }
  293. else
  294. {
  295. scrollfocus = ss$('html');
  296. }
  297. for (var i = nodelist.length-1; i >= 0 ; i--) {
  298. //var parent = nodelist[i-1];
  299. var newfocus = nodelist[i];
  300. if (DEBUG)
  301. {
  302. ss$(newfocus).css( "border", "1px solid blue" );
  303. //var debugtimer = setTimeout(function(){ ss$(newfocus).css( "border", "0px solid white" ); }, 1000);
  304. }
  305. //if (ss$(newfocus).hasScrollBar3() && hasScrollBarVisible(newfocus) && canscroll(newfocus, dir) && hasscrollbars(newfocus))
  306. if (canscroll(newfocus, dir) && hasscrollbars(newfocus))
  307. {
  308. scrollfocus = ss$(newfocus);
  309. return newfocus;
  310. }
  311. }
  312.  
  313. return scrollfocus;
  314. }
  315. ss$('html').bind({
  316. //mousewheel: function(e) {
  317. //if (DEBUG)
  318. //{
  319. // console.log(scrollfocus);
  320. //}
  321. //console.log("scrolling");
  322. //MouseScroll(e.originalEvent);
  323. //},
  324. mousemove: function(e) {
  325. mousemoved = true;
  326. },
  327. mousedown: function(e) {
  328. if (DEBUG)
  329. {
  330. console.log(scrollfocus);
  331. }
  332. if (scrollfocus)
  333. {
  334. ss$(scrollfocus)[0].setAttribute( "positiondelta",0 );
  335. ss$(scrollfocus).stop();
  336. }
  337. scrollfocus = UpdateFocus(0,0);
  338. console.log("click");
  339. }
  340. });
  341. //Init(window);
  342.  
  343. document.documentElement.addEventListener("wheel", function(e){
  344. MouseScroll(e);
  345. //console.log("scrolling");
  346. });
  347. /*var oldpos = ss$('body').scrollTop();
  348. ss$('body').scrollTop(1);
  349. var iswebkit = ss$('body').scrollTop()>0;
  350. ss$('body').scrollTop(oldpos);*/
  351.  
  352. //WEBKIT = 'webkitRequestAnimationFrame' in window;
  353. WEBKIT = document.compatMode == 'CSS1Compat'
  354. //console.log("window: " + window);
  355. console.log("Smoothscroll initiated! Webkit: " + WEBKIT);
  356. }
  357.  
  358. var JQUERY = false;
  359. var OWNJQUERY = false;
  360. var OLDJQUERY = false;
  361.  
  362. var old$;
  363. var oldjQuery;
  364.  
  365. var ss$;
  366.  
  367. function jQueryVersion(temp$)
  368. {
  369. if (typeof temp$ == 'undefined' || typeof temp$.fn == 'undefined' || typeof temp$.fn.jquery == 'undefined')
  370. {
  371. return 0;
  372. }
  373.  
  374. var versiontable = temp$.fn.jquery.split('.');
  375. var version = 0;
  376. for (var i = versiontable.length-1; i >= 0; i--) {
  377. var power = versiontable.length-i;
  378. version += Math.pow(10,power-1)*versiontable[i];
  379. }
  380. return version;
  381. }
  382.  
  383.  
  384. function LoadConfig()
  385. {
  386. smoothness = GM_getValue( 'smoothness', smoothness );
  387. sensitivity = GM_getValue( 'sensitivity', sensitivity );
  388. acceleration = GM_getValue( 'acceleration', acceleration );
  389. baserefreshrate = GM_getValue( 'baserefreshrate', baserefreshrate );
  390. //alternative_sesitivity_multiplier = GM_getValue( 'alternative_sesitivity_multiplier', alternative_sesitivity_multiplier );
  391. console.log("Config for smoothscroll loaded!")
  392. }
  393.  
  394. function SaveConfig()
  395. {
  396. GM_setValue( 'smoothness', document.getElementById('ss-smoothness').value)
  397. GM_setValue( 'sensitivity', document.getElementById('ss-sensitivity').value)
  398. GM_setValue( 'acceleration', document.getElementById('ss-acceleration').value)
  399. GM_setValue( 'baserefreshrate', document.getElementById('ss-baserefreshrate').value)
  400. //console.log(document.getElementById('ss-alternative_sesitivity_multiplier').checked)
  401. console.log("Config for smoothscroll saved!")
  402. }
  403.  
  404. function InitConfigmenu()
  405. {
  406. console.log("Initiating smoothscroll config...");
  407. var configbar = document.createElement('div');
  408. configbar.setAttribute("id","ss-configbar");
  409. configbar.innerHTML = '<div style="display:block; width: 100%; height: auto; border: 0px solid #aaaaaa; background-color: grey;">Config page for smoothscroll (refresh page for changes to apply!) Made by: Creec Winceptor</div><div id="ss-config" style="margin:3px; display:block; width: auto; height: auto; border: 0px solid #554433;"><table style="width:auto;"><tr><td>Smoothness</td><td><input type="number" id="ss-smoothness" min="0" max="100" value="' + smoothness + '"></td><td> Smoothness factor value (how strong the smoothing effect is)</td></tr><tr><td>Sensitivity</td><td><input type="number" id="ss-sensitivity" min="0" max="100" value="' + sensitivity + '"></td><td> Scroll sensitivity (duh)</td></tr><tr><td>Acceleration</td><td><input type="number" id="ss-acceleration" min="0" max="100" value="' + acceleration + '"></td><td> Acceleration of continuous scroll action (saves your finger)</td></tr><tr><td>Refreshrate</td><td><input type="number" id="ss-baserefreshrate" min="1" max="100" value="' + baserefreshrate + '"></td><td>Refreshrate of scrollanimation (60Hz default)</td></tr></table></div><div style="width: 100%; height: auto; text-align: center; background-color: grey;"><input id="ss-save" type="button" value="Save config" style="width: 44%; height: auto; border: 0px solid #aaaaaa; margin: 3px"/><input id="ss-close" type="button" value="Close config" style="width: 44%; height: auto; border: 0px solid #aaaaaa; margin: 3px"/><div>';
  410. var configparent = document.getElementsByTagName("body")[0];
  411. configbar.style.width = '100%';
  412. configbar.style.display = 'none';
  413. configbar.style.position = 'absolute';
  414. configbar.style.zIndex = '9999';
  415. configbar.style.backgroundColor = 'white';
  416. configparent.insertBefore(configbar, configparent.childNodes[0]);
  417. ss$("#ss-close").click(function() {
  418. //ss$("#ss-config").animate({
  419. // height: '0'
  420. //}, 100);
  421. ss$("#ss-configbar").hide("slow");
  422. });
  423. ss$("#ss-save").click(function() {
  424. SaveConfig();
  425. });
  426. //ss$("#ss-configbar").hide();
  427. }
  428.  
  429. function ConfigSmoothscroll()
  430. {
  431. if (typeof ss$ == 'undefined'){
  432. alert("Smoothscroll is not running properly on this page!");
  433. return;
  434. }
  435. //ss$("#ss-config").animate({
  436. // height: '100px'
  437. //}, 100);
  438. ss$("#ss-configbar").show("slow");
  439. ss$("html, body").animate({ scrollTop: 0 }, "slow");
  440. }
  441.  
  442. function LoadjQuery(loading)
  443. {
  444. if (loading)
  445. {
  446. console.log("Waiting for jQuery...");
  447. if (typeof $ == 'undefined' || typeof jQuery == 'undefined' || jQueryVersion($)<minimal_jquery_version)
  448. {
  449. if (jquery_retry_count<jquery_retry_max)
  450. {
  451. jquery_retry_count++;
  452. setTimeout(function() {
  453. LoadjQuery(true);
  454. }, 100);
  455. }
  456. else
  457. {
  458. console.log("Failed to load smoothscroll!");
  459. if (typeof old$ != 'undefined') {
  460. $ = old$;
  461. }
  462. if (typeof oldjQuery != 'undefined') {
  463. jQuery = oldjQuery;
  464. }
  465. }
  466. }
  467. else
  468. {
  469. ss$ = $;
  470. ssjQuery = jQuery;
  471. console.log("jQuery loaded!");
  472. if (typeof old$ != 'undefined') {
  473. $ = old$;
  474. }
  475. if (typeof oldjQuery != 'undefined') {
  476. jQuery = oldjQuery;
  477. }
  478. console.log("Page jQuery version: " + jQueryVersion(old$));
  479. console.log("Script jQuery version: " + jQueryVersion(ss$));
  480. InitSmoothscroll();
  481. }
  482. }
  483. else
  484. {
  485. console.log("Loading own jQuery...");
  486. jquery_retry_count = 0;
  487. var filename = "https://code.jquery.com/jquery-2.2.3.js";
  488. var fileref = document.createElement('script');
  489. fileref.setAttribute("type","text/javascript");
  490. fileref.setAttribute("src", filename);
  491.  
  492. if (typeof fileref!="undefined")
  493. {
  494. var scriptparent = document.getElementsByTagName("head")[0];
  495.  
  496. if (typeof scriptparent=="undefined")
  497. {
  498. var scriptparent = document.createElement('head');
  499. document.getElementsByTagName("html")[0].appendChild(scriptparent);
  500. }
  501. scriptparent.appendChild(fileref);
  502. }
  503. LoadjQuery(true);
  504. }
  505. }
  506.  
  507.  
  508. function Init()
  509. {
  510.  
  511. if (typeof ss$ == 'undefined' )
  512. {
  513. var sitejquery = !(typeof $ == 'undefined' || typeof jQuery == 'undefined');
  514. if (sitejquery && jQueryVersion($)>=minimal_jquery_version)
  515. {
  516. ss$ = $;
  517. ssjQuery = jQuery;
  518. console.log("Reusing page jQuery...");
  519. console.log("Page jQuery version: " + jQueryVersion($));
  520. InitSmoothscroll();
  521. }
  522. else
  523. {
  524. if (typeof $ != 'undefined' && typeof old$ == 'undefined')
  525. {
  526. old$ = $;
  527. }
  528. if (typeof jQuery != 'undefined' && typeof oldjQuery == 'undefined')
  529. {
  530. oldjQuery = jQuery;
  531. }
  532. LoadjQuery(false);
  533. }
  534. }
  535. }
  536.  
  537. if (typeof ss$ == 'undefined'){
  538. console.log("Initiating smoothscroll...");
  539. Init();
  540. //InitSmoothscroll();
  541. GM_registerMenuCommand("Configurate smoothscroll", ConfigSmoothscroll);
  542. }
  543. else
  544. {
  545. console.log("Smoothscroll already loaded!");
  546. }