From 8d3295458c2ffaf0e2fa01279f0e5e14930f9e6e Mon Sep 17 00:00:00 2001
From: Adam Wolf <adamwolf@feelslikeburning.com>
Date: Sat, 3 Oct 2020 14:57:55 -0500
Subject: [PATCH] Add a bookmarklet

The bookmarklet lets you quickly open the Add page
with the URL already populated in the URLs box.
---
 archivebox/core/views.py                 | 11 +++++++++++
 archivebox/themes/default/add_links.html |  6 ++++++
 2 files changed, 17 insertions(+)

diff --git a/archivebox/core/views.py b/archivebox/core/views.py
index 4144b2db..7cd8b104 100644
--- a/archivebox/core/views.py
+++ b/archivebox/core/views.py
@@ -114,12 +114,23 @@ class AddView(UserPassesTestMixin, FormView):
     template_name = "add_links.html"
     form_class = AddLinkForm
 
+    def get_initial(self):
+        """Prefill the AddLinkForm with the 'url' GET parameter"""
+        if self.request.method == 'GET':
+            url = self.request.GET.get('url', None)
+            if url:
+                return {'url': url}
+        else:
+            return super().get_initial()
+
     def test_func(self):
         return PUBLIC_ADD_VIEW or self.request.user.is_authenticated
 
     def get_context_data(self, *args, **kwargs):
         context = super().get_context_data(*args, **kwargs)
         context["title"] = "Add URLs"
+        # We can't just call request.build_absolute_uri in the template, because it would include query parameters
+        context["absolute_add_path"] = self.request.build_absolute_uri(self.request.path)
         return context
 
     def form_valid(self, form):
diff --git a/archivebox/themes/default/add_links.html b/archivebox/themes/default/add_links.html
index cb6f4341..0b384f5c 100644
--- a/archivebox/themes/default/add_links.html
+++ b/archivebox/themes/default/add_links.html
@@ -49,6 +49,12 @@
                     <small>(it's safe to leave this page, adding will continue in the background)</small>
                 </div>
             </center>
+            {% if absolute_add_path %}
+            <center id="bookmarklet">
+              <p>Bookmark this link to quickly add to your archive:
+                <a href="javascript:void(window.open('{{ absolute_add_path }}?url='+document.location.href));">Add to ArchiveBox</a></p>
+            </center>
+            {% endif %}
             <script>
                 document.getElementById('add-form').addEventListener('submit', function(event) {
                     setTimeout(function() {