infobar.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright 2018 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 browser, document, Node */
  21. this.singlefile.infobar = this.singlefile.infobar || (() => {
  22. const INFOBAR_TAGNAME = "singlefile-infobar";
  23. const LINK_ICON = "<svg style=\"vertical-align: middle\" xmlns=\"http://www.w3.org/2000/svg\" width=\"18\" height=\"18\" viewBox=\"0 0 24 24\" fill=\"#9AA0A6\"><path d=\"M19 19H5V5h7V3H5a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14c1.1 0 2-.9 2-2v-7h-2v7zM14 3v2h3.59l-9.83 9.83 1.41 1.41L19 6.41V10h2V3h-7z\"/></svg>";
  24. const SINGLEFILE_COMMENT = "Archive processed by SingleFile";
  25. if (window == top) {
  26. document.addEventListener("DOMContentLoaded", async () => {
  27. const singleFileComment = document.documentElement.childNodes[0];
  28. if (singleFileComment.nodeType == Node.COMMENT_NODE && singleFileComment.textContent.includes(SINGLEFILE_COMMENT)) {
  29. const info = singleFileComment.textContent.split("\n");
  30. const [, , url, saveDate] = info;
  31. const config = await browser.runtime.sendMessage({ getConfig: true });
  32. if (config.displayInfobar) {
  33. initInfobar(url, saveDate);
  34. }
  35. }
  36. });
  37. }
  38. return true;
  39. function initInfobar(url, saveDate) {
  40. let infobarElement = document.querySelector(INFOBAR_TAGNAME);
  41. if (!infobarElement) {
  42. infobarElement = document.createElement(INFOBAR_TAGNAME);
  43. infobarElement.style.all = "unset";
  44. infobarElement.style.display = "block";
  45. infobarElement.style.fontSize = "15px";
  46. infobarElement.style.color = "#9aa0a6";
  47. infobarElement.style.position = "fixed";
  48. infobarElement.style.top = "16px";
  49. infobarElement.style.right = "16px";
  50. infobarElement.style.height = "auto";
  51. infobarElement.style.width = "32px";
  52. infobarElement.style.lineHeight = "32px";
  53. infobarElement.style.borderRadius = "16px";
  54. infobarElement.style.border = "2px solid #737373";
  55. infobarElement.style.zIndex = 2147483647;
  56. infobarElement.style.textAlign = "center";
  57. infobarElement.style.transition = "all 250ms";
  58. const linkElement = document.createElement("a");
  59. linkElement.style.all = "unset";
  60. linkElement.style.display = "inline-block";
  61. linkElement.style.paddingLeft = "8px";
  62. linkElement.style.lineHeight = "32px";
  63. linkElement.style.cursor = "pointer";
  64. linkElement.target = "_blank";
  65. linkElement.rel = "noopener noreferrer";
  66. linkElement.title = "Open original page";
  67. linkElement.href = url.split("url: ")[1];
  68. linkElement.innerHTML = LINK_ICON;
  69. hideInfobar(infobarElement, linkElement, saveDate);
  70. infobarElement.onmouseover = () => infobarElement.style.opacity = 1;
  71. document.body.appendChild(infobarElement);
  72. document.addEventListener("click", event => {
  73. let element = event.target;
  74. while (element && element != infobarElement) {
  75. element = element.parentElement;
  76. }
  77. if (element != infobarElement) {
  78. hideInfobar(infobarElement, linkElement, saveDate);
  79. }
  80. });
  81. }
  82. }
  83. function displayInfobar(infobarElement, linkElement, saveDate) {
  84. infobarElement.style.opacity = 1;
  85. infobarElement.onmouseout = null;
  86. infobarElement.style.paddingLeft = infobarElement.style.paddingRight = "16px";
  87. infobarElement.textContent = saveDate.split("saved date: ")[1];
  88. infobarElement.style.width = "auto";
  89. infobarElement.style.backgroundColor = "#f9f9f9";
  90. infobarElement.style.cursor = "auto";
  91. infobarElement.appendChild(linkElement);
  92. infobarElement.onclick = null;
  93. }
  94. function hideInfobar(infobarElement, linkElement, saveDate) {
  95. infobarElement.style.opacity = .7;
  96. infobarElement.onmouseout = () => infobarElement.style.opacity = .7;
  97. infobarElement.style.paddingLeft = infobarElement.style.paddingRight = "0px";
  98. infobarElement.style.width = "32px";
  99. infobarElement.style.backgroundColor = "#737373";
  100. infobarElement.style.cursor = "pointer";
  101. infobarElement.textContent = "❔";
  102. infobarElement.onclick = event => {
  103. if (event.button === 0) {
  104. displayInfobar(infobarElement, linkElement, saveDate);
  105. return false;
  106. }
  107. };
  108. }
  109. })();