openLocation.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. var browser;
  6. var dialog = {};
  7. var pref = null;
  8. var openLocationModule = {};
  9. try {
  10. pref = Components.classes["@mozilla.org/preferences-service;1"]
  11. .getService(Components.interfaces.nsIPrefBranch);
  12. } catch(ex) {
  13. // not critical, remain silent
  14. }
  15. Components.utils.import("resource:///modules/openLocationLastURL.jsm", openLocationModule);
  16. var gOpenLocationLastURL = new openLocationModule.OpenLocationLastURL(window.opener);
  17. function onLoad() {
  18. dialog.input = document.getElementById("dialog.input");
  19. dialog.open = document.documentElement.getButton("accept");
  20. dialog.openWhereList = document.getElementById("openWhereList");
  21. dialog.openTopWindow = document.getElementById("currentWindow");
  22. dialog.bundle = document.getElementById("openLocationBundle");
  23. if ("arguments" in window && window.arguments.length >= 1) {
  24. browser = window.arguments[0];
  25. }
  26. dialog.openWhereList.selectedItem = dialog.openTopWindow;
  27. if (pref) {
  28. try {
  29. var useAutoFill = pref.getBoolPref("browser.urlbar.autoFill");
  30. if (useAutoFill) {
  31. dialog.input.setAttribute("completedefaultindex", "true");
  32. }
  33. } catch(ex) {}
  34. try {
  35. var value = pref.getIntPref("general.open_location.last_window_choice");
  36. var element = dialog.openWhereList.getElementsByAttribute("value", value)[0];
  37. if (element) {
  38. dialog.openWhereList.selectedItem = element;
  39. }
  40. dialog.input.value = gOpenLocationLastURL.value;
  41. } catch(ex) {}
  42. if (dialog.input.value) {
  43. // XXX should probably be done automatically
  44. dialog.input.select();
  45. }
  46. }
  47. doEnabling();
  48. }
  49. function doEnabling() {
  50. dialog.open.disabled = !dialog.input.value;
  51. }
  52. function open() {
  53. var openData = {
  54. "url": null,
  55. "postData": null,
  56. "mayInheritPrincipal": false
  57. };
  58. if (browser) {
  59. browser.getShortcutOrURIAndPostData(dialog.input.value).then(data => {
  60. openData.url = data.url;
  61. openData.postData = data.postData;
  62. openData.mayInheritPrincipal = data.mayInheritPrincipal;
  63. openLocation(openData);
  64. });
  65. } else {
  66. openData.url = dialog.input.value;
  67. openLocation(openData);
  68. }
  69. return false;
  70. }
  71. function openLocation(openData) {
  72. try {
  73. // Whichever target we use for the load, we allow third-party services to
  74. // fix up the URI
  75. switch (dialog.openWhereList.value) {
  76. case "0":
  77. var webNav = Components.interfaces.nsIWebNavigation;
  78. var flags = webNav.LOAD_FLAGS_ALLOW_THIRD_PARTY_FIXUP |
  79. webNav.LOAD_FLAGS_FIXUP_SCHEME_TYPOS;
  80. if (!openData.mayInheritPrincipal) {
  81. flags |= webNav.LOAD_FLAGS_DISALLOW_INHERIT_PRINCIPAL;
  82. }
  83. browser.gBrowser.loadURIWithFlags(openData.url, flags, null, null, openData.postData);
  84. break;
  85. case "1":
  86. window.opener.delayedOpenWindow(getBrowserURL(), "all,dialog=no",
  87. openData.url, openData.postData,
  88. null, null, true);
  89. break;
  90. case "3":
  91. browser.delayedOpenTab(openData.url, null, null, openData.postData, true);
  92. break;
  93. }
  94. } catch(ex) {}
  95. if (pref) {
  96. gOpenLocationLastURL.value = dialog.input.value;
  97. pref.setIntPref("general.open_location.last_window_choice", dialog.openWhereList.value);
  98. }
  99. window.close();
  100. }
  101. function createInstance(contractid, iidName)
  102. {
  103. var iid = Components.interfaces[iidName];
  104. return Components.classes[contractid].createInstance(iid);
  105. }
  106. const nsIFilePicker = Components.interfaces.nsIFilePicker;
  107. function onChooseFile()
  108. {
  109. try {
  110. let fp = Components.classes["@mozilla.org/filepicker;1"]
  111. .createInstance(nsIFilePicker);
  112. let fpCallback = function fpCallback_done(aResult) {
  113. if (aResult == nsIFilePicker.returnOK &&
  114. fp.fileURL.spec &&
  115. fp.fileURL.spec.length > 0) {
  116. dialog.input.value = fp.fileURL.spec;
  117. }
  118. doEnabling();
  119. };
  120. fp.init(window, dialog.bundle.getString("chooseFileDialogTitle"),
  121. nsIFilePicker.modeOpen);
  122. fp.appendFilters(nsIFilePicker.filterAll | nsIFilePicker.filterText |
  123. nsIFilePicker.filterImages | nsIFilePicker.filterXML |
  124. nsIFilePicker.filterHTML);
  125. fp.open(fpCallback);
  126. } catch(ex) {}
  127. }