banner.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 link = document.getElementById("link");
  22. var filenameInput = document.getElementById("filename");
  23. var closeButton = document.getElementById("close");
  24. var editButton = document.getElementById("edit");
  25. var params = location.search.substring(1).split("&");
  26. var date = new Date();
  27. var time = date.toISOString().split("T")[0] + " " + date.toLocaleTimeString();
  28. var filename = decodeURIComponent(params[1]) + " (" + time + ")" + ".htm";
  29. function close() {
  30. chrome.extension.sendRequest({
  31. closeBanner : true
  32. });
  33. }
  34. function resetFilename() {
  35. filenameInput.style.textOverflow = "ellipsis";
  36. filenameInput.blur();
  37. filenameInput.contentEditable = false;
  38. filenameInput.removeEventListener("keydown", onkeydown, false);
  39. }
  40. function onkeydown(event) {
  41. if (event.keyIdentifier == "U+001B") {
  42. resetFilename();
  43. filenameInput.textContent = filenameInput.title = filename;
  44. }
  45. if (event.keyIdentifier == "Enter") {
  46. resetFilename();
  47. filename = link.download = filenameInput.title = filenameInput.textContent;
  48. event.preventDefault();
  49. }
  50. }
  51. function editName() {
  52. filenameInput.style["text-overflow"] = "clip";
  53. filenameInput.contentEditable = true;
  54. filenameInput.focus();
  55. filenameInput.addEventListener("keydown", onkeydown, false);
  56. }
  57. link.href = decodeURIComponent(params[0]);
  58. link.download = filenameInput.textContent = filenameInput.title = filename;
  59. link.addEventListener("click", close, false);
  60. closeButton.addEventListener("click", close, false);
  61. editButton.addEventListener("click", editName, false);
  62. })();