web-lazy-loader-before.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Copyright 2010-2019 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * SingleFile is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SingleFile is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with SingleFile. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /* global screen, window, document, dispatchEvent, UIEvent, CustomEvent, Element */
  21. (() => {
  22. const LOAD_OBSERVED_ELEMENTS_EVENT = "single-file-load-observed-elements";
  23. const clientHeight = document.documentElement.clientHeight;
  24. const clientWidth = document.documentElement.clientWidth;
  25. const scrollHeight = Math.max(document.documentElement.scrollHeight - (clientHeight * .5), clientHeight);
  26. const scrollWidth = Math.max(document.documentElement.scrollWidth - (clientWidth * .5), clientWidth);
  27. window._singleFile_innerHeight = window.innerHeight;
  28. window._singleFile_innerWidth = window.innerWidth;
  29. window.__defineGetter__("innerHeight", () => scrollHeight);
  30. window.__defineGetter__("innerWidth", () => scrollWidth);
  31. document.documentElement.__defineGetter__("clientHeight", () => scrollHeight);
  32. document.documentElement.__defineGetter__("clientWidth", () => scrollWidth);
  33. screen.__defineGetter__("height", () => scrollHeight);
  34. screen.__defineGetter__("width", () => scrollWidth);
  35. window._singleFile_getBoundingClientRect = Element.prototype.getBoundingClientRect;
  36. Element.prototype.getBoundingClientRect = function () {
  37. const boundingRect = window._singleFile_getBoundingClientRect.call(this);
  38. if (this == document.documentElement) {
  39. boundingRect.__defineGetter__("height", () => scrollHeight);
  40. boundingRect.__defineGetter__("bottom", () => scrollHeight + boundingRect.top);
  41. boundingRect.__defineGetter__("width", () => scrollWidth);
  42. boundingRect.__defineGetter__("right", () => scrollWidth + boundingRect.left);
  43. }
  44. return boundingRect;
  45. };
  46. window._singleFile_localStorage = window.localStorage;
  47. window.__defineGetter__("localStorage", () => { throw new Error("localStorage temporary blocked by SingleFile"); });
  48. document.__defineGetter__("cookie", () => { throw new Error("document.cookie temporary blocked by SingleFile"); });
  49. dispatchEvent(new UIEvent("resize"));
  50. dispatchEvent(new UIEvent("scroll"));
  51. dispatchEvent(new CustomEvent(LOAD_OBSERVED_ELEMENTS_EVENT));
  52. })();