content.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright 2011 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. (function() {
  21. var MASK_ID = "__SingleFile_mask__", topWindow = window == top;
  22. function processStart() {
  23. var div = document.getElementById(MASK_ID);
  24. if (!div) {
  25. div = document.createElement("div");
  26. div.id = "__SingleFile_mask__";
  27. div.style.position = "fixed";
  28. div.style.top = "0px";
  29. div.style.left = "0px";
  30. div.style.height = "100%";
  31. div.style.width = "100%";
  32. div.style.backgroundColor = "black";
  33. div.style.zIndex = 2147483647;
  34. div.style.opacity = 0;
  35. div.style["-webkit-transition"] = "opacity 250ms";
  36. document.body.appendChild(div);
  37. div.offsetWidth;
  38. div.style.opacity = .3;
  39. }
  40. }
  41. function processEnd() {
  42. var div = document.getElementById(MASK_ID);
  43. if (div)
  44. document.body.removeChild(div);
  45. }
  46. function displayBanner(url) {
  47. var frame = document.createElement("iframe");
  48. frame.style.width = "100%";
  49. frame.style.position = "fixed";
  50. frame.style.top = "0px";
  51. frame.style.left = "0px";
  52. frame.style.borderWidth = "0px";
  53. frame.style.borderBottomWidth = "1px";
  54. frame.style.borderBottomStyle = "solid";
  55. frame.style.borderBottomColor = "#b6bac0";
  56. frame.style.zIndex = 2147483647;
  57. frame.style["-webkit-transition"] = "height .5s ease-out";
  58. frame.src = url;
  59. frame.id = "singlefile-save-banner";
  60. document.documentElement.style["-webkit-transition"] = "top .5s ease-out";
  61. document.documentElement.style.position = "relative";
  62. document.body.appendChild(frame);
  63. frame.style.height = "0px";
  64. frame.offsetLeft;
  65. document.documentElement.style.top = "36px";
  66. frame.style.height = "35px";
  67. }
  68. function closeBanner() {
  69. document.documentElement.style["-webkit-transition-duration"] = "0s";
  70. document.documentElement.style.top = "0px";
  71. document.body.removeChild(document.getElementById("singlefile-save-banner"));
  72. }
  73. window.addEventListener("keyup", function(event) {
  74. if (event.ctrlKey && event.shiftKey && event.keyCode == 83)
  75. chrome.extension.sendMessage({});
  76. }, true);
  77. if (topWindow) {
  78. chrome.extension.onMessage.addListener(function(request) {
  79. if (request.processStart)
  80. processStart();
  81. if (request.processEnd)
  82. processEnd();
  83. if (request.displayBanner)
  84. displayBanner(request.url);
  85. if (request.closeBanner)
  86. closeBanner();
  87. });
  88. }
  89. })();