Repository: qutebrowser/qutebrowser

Issue:
Support adding bookmarks by url
Please remember to mention your version info (qutebrowser, Qt, PyQt,
OS/distribution) from the `qute:version` page or `qutebrowser --version`

---

(I am using Qutebrowser version 0.7.0)
# Summary

Is there any way of adding a bookmark by just entering the url?
# What I'm trying to do

I have a url (let's call it url1) that does some magic (probably with cookies) and then redirects to another webpage (let's call it url2). Doing `:bookmark-add` whilst on url2 does save url2 as a bookmark, but the magic that happens when url1 is clicked doesn't happen.

Just allowing bookmark-add to accept an optional url parameter would probs be the easiest way of doing this.



Discussion:
This probably makes sense, but automatically retrieving the title will probably be a bit tricky.

cc @antoyo 

Could set the title to "" (ie blank) until first load. Or allow the user to set one when creating the bookmark, which is what other browsers do.

So, in other words:
- `:bookmark-add` adds the current webpage as a bookmark with the page title as a title.
- `:bookmark-add some-url some-title` adds some-url as a bookmark with some-title as the title.
- And `:bookmark-add some-url` could either show an error saying a title is required, set the title to "", or create a title eg "Untitled bookmark 1".


Resolving patch:
diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py
--- a/qutebrowser/browser/commands.py
+++ b/qutebrowser/browser/commands.py
@@ -1116,12 +1116,33 @@ def quickmark_load(self, name, tab=False, bg=False, window=False):
         self._open(url, tab, bg, window)
 
     @cmdutils.register(instance='command-dispatcher', scope='window')
-    def bookmark_add(self):
-        """Save the current page as a bookmark."""
+    def bookmark_add(self, url=None, title=None):
+        """Save the current page as a bookmark, or a specific url.
+
+        If no url and title are provided, then save the current page as a
+        bookmark.
+        If a url and title have been provided, then save the given url as
+        a bookmark with the provided title.
+
+        Args:
+            url: url to save as a bookmark. If None, use url of current page.
+            title: title of the new bookmark.
+        """
+        if url and not title:
+            raise cmdexc.CommandError('Title must be provided if url has '
+                                      'been provided')
         bookmark_manager = objreg.get('bookmark-manager')
-        url = self._current_url()
+        if url is None:
+            url = self._current_url()
+        else:
+            try:
+                url = urlutils.fuzzy_url(url)
+            except urlutils.InvalidUrlError as e:
+                raise cmdexc.CommandError(e)
+        if not title:
+            title = self._current_title()
         try:
-            bookmark_manager.add(url, self._current_title())
+            bookmark_manager.add(url, title)
         except urlmarks.Error as e:
             raise cmdexc.CommandError(str(e))
         else:


Test patch:
diff --git a/tests/end2end/features/urlmarks.feature b/tests/end2end/features/urlmarks.feature
--- a/tests/end2end/features/urlmarks.feature
+++ b/tests/end2end/features/urlmarks.feature
@@ -8,6 +8,20 @@ Feature: quickmarks and bookmarks
         Then the message "Bookmarked http://localhost:*/data/title.html!" should be shown
         And the bookmark file should contain "http://localhost:*/data/title.html Test title"
 
+    Scenario: Saving a bookmark with a provided url and title
+        When I run :bookmark-add http://example.com "some example title"
+        Then the message "Bookmarked http://example.com!" should be shown
+        And the bookmark file should contain "http://example.com some example title"
+
+    Scenario: Saving a bookmark with a url but no title
+        When I run :bookmark-add http://example.com
+        Then the error "Title must be provided if url has been provided" should be shown
+
+    Scenario: Saving a bookmark with an invalid url
+        When I set general -> auto-search to false
+        And I run :bookmark-add foo! "some example title"
+        Then the error "Invalid URL" should be shown
+
     Scenario: Saving a duplicate bookmark
         Given I have a fresh instance
         When I open data/title.html