Bug 1474179 - Part 2: Adds "copy raw" menuitem to the popup menu of the URL bar. r?valentin draft
authorZhang Junzhi <zjz@zjz.name>
Thu, 12 Jul 2018 21:46:40 +0800
changeset 817381 263ab29e962de647d2b2779f2a1847e9d2ba705b
parent 817380 5d8b3e628db7aa62a498f1b27c08f327e57c81c0
child 817382 67555daed546460cae7c1eed4745c3652006cc43
push id116050
push userbmo:zjz@zjz.name
push dateThu, 12 Jul 2018 16:06:02 +0000
reviewersvalentin
bugs1474179
milestone63.0a1
Bug 1474179 - Part 2: Adds "copy raw" menuitem to the popup menu of the URL bar. r?valentin The "copy raw" menuitem is available in the menu only when the URL of the loaded page is encoded and the entire of the URL is selected. This way, we provide an option for the user to copy the raw URL instead of the decoded one. MozReview-Commit-ID: 5HLfSlEH4eU
browser/base/content/urlbarBindings.xml
browser/locales/en-US/chrome/browser/browser.properties
--- a/browser/base/content/urlbarBindings.xml
+++ b/browser/base/content/urlbarBindings.xml
@@ -88,26 +88,45 @@ file, You can obtain one at http://mozil
         this.inputField.addEventListener("mouseout", this);
         this.inputField.addEventListener("overflow", this);
         this.inputField.addEventListener("underflow", this);
 
         var textBox = document.getAnonymousElementByAttribute(this,
                                                 "anonid", "textbox-input-box");
         var cxmenu = document.getAnonymousElementByAttribute(textBox,
                                             "anonid", "input-box-contextmenu");
-        var pasteAndGo;
-        cxmenu.addEventListener("popupshowing", function() {
-          if (!pasteAndGo)
-            return;
-          var controller = document.commandDispatcher.getControllerForCommand("cmd_paste");
-          var enabled = controller.isCommandEnabled("cmd_paste");
-          if (enabled)
-            pasteAndGo.removeAttribute("disabled");
-          else
-            pasteAndGo.setAttribute("disabled", "true");
+        let pasteAndGo;
+        let copyRaw;
+        cxmenu.addEventListener("popupshowing", () => {
+          if (pasteAndGo) {
+            let controller = document.commandDispatcher.getControllerForCommand("cmd_paste");
+            let enabled = controller.isCommandEnabled("cmd_paste");
+            if (enabled)
+              pasteAndGo.removeAttribute("disabled");
+            else
+              pasteAndGo.setAttribute("disabled", "true");
+          }
+          if (copyRaw) {
+            // There are 4 cases where there's no need to display the "copy raw" menu item:
+            // 1. If the text in the URL bar is different from the loaded page
+            //    (for example, the user has edited the text after page load);
+            // 2. If the text in the URL bar URL isn't fully selected;
+            // 3. If the scheme of the URL is data: or javascript:;
+            // 4. If the URL is already raw(i.e. a URL which hasn't been decoded to Unicode).
+            if (this.getAttribute("pageproxystate") !== "valid" ||
+                this.selectionStart !== 0 ||
+                this.selectionEnd !== this.inputField.value.length ||
+                gBrowser.currentURI.schemeIs("javascript") ||
+                gBrowser.currentURI.schemeIs("data") ||
+                gBrowser.currentURI.asciiSpec.includes(this.inputField.value)) {
+              copyRaw.setAttribute("hidden", "true");
+            } else {
+              copyRaw.removeAttribute("hidden");
+            }
+          }
         });
 
         var insertLocation = cxmenu.firstChild;
         while (insertLocation.nextSibling &&
                insertLocation.getAttribute("cmd") != "cmd_paste")
           insertLocation = insertLocation.nextSibling;
         if (insertLocation) {
           pasteAndGo = document.createElement("menuitem");
@@ -115,16 +134,34 @@ file, You can obtain one at http://mozil
                                    GetStringFromName("pasteAndGo.label");
           pasteAndGo.setAttribute("label", label);
           pasteAndGo.setAttribute("anonid", "paste-and-go");
           pasteAndGo.setAttribute("oncommand",
               "gURLBar.select(); goDoCommand('cmd_paste'); gURLBar.handleCommand();");
           cxmenu.insertBefore(pasteAndGo, insertLocation.nextSibling);
         }
 
+        insertLocation = cxmenu.firstChild;
+        while (insertLocation.nextSibling &&
+               insertLocation.getAttribute("cmd") != "cmd_copy")
+          insertLocation = insertLocation.nextSibling;
+        if (insertLocation) {
+          copyRaw = document.createElement("menuitem");
+          let label = Services.strings.createBundle("chrome://browser/locale/browser.properties").
+                                   GetStringFromName("copyRaw.label");
+          copyRaw.setAttribute("label", label);
+          copyRaw.setAttribute("anonid", "copy-raw");
+          copyRaw.setAttribute("accesskey", "r");
+          copyRaw.setAttribute("oncommand",
+              `Cc["@mozilla.org/widget/clipboardhelper;1"]
+                 .getService(Ci.nsIClipboardHelper)
+                 .copyString(gBrowser.currentURI.asciiSpec);`);
+          cxmenu.insertBefore(copyRaw, insertLocation.nextSibling);
+        }
+
         this.popup.addEventListener("popupshowing", () => {
           this._enableOrDisableOneOffSearches();
         }, {capture: true, once: true});
 
         // history dropmarker open state
         this.popup.addEventListener("popupshowing", () => {
           this.setAttribute("open", "true");
         });
--- a/browser/locales/en-US/chrome/browser/browser.properties
+++ b/browser/locales/en-US/chrome/browser/browser.properties
@@ -397,16 +397,17 @@ menuUndoCloseWindowLabel=#1 (and #2 othe
 menuUndoCloseWindowSingleTabLabel=#1
 
 # Unified Back-/Forward Popup
 tabHistory.current=Stay on this page
 tabHistory.goBack=Go back to this page
 tabHistory.goForward=Go forward to this page
 
 # URL Bar
+copyRaw.label=Copy Raw
 pasteAndGo.label=Paste & Go
 # LOCALIZATION NOTE (reloadButton.tooltip):
 # %S is the keyboard shortcut for reloading the current page
 reloadButton.tooltip=Reload current page (%S)
 # LOCALIZATION NOTE (stopButton.tooltip):
 # %S is the keyboard shortcut for stopping loading the page
 stopButton.tooltip=Stop loading this page (%S)
 # LOCALIZATION NOTE (urlbar-zoom-button.tooltip):