infobar.js 4.6 KB

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