Merge pull request #460 from rogeliodh/add_bookmarklet

add bookmarklet (#385)
This commit is contained in:
Jana 2020-07-24 08:09:15 +05:30 committed by GitHub
commit 9e85b59a35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 0 deletions

18
bukuserver/bookmarklet.js Normal file
View File

@ -0,0 +1,18 @@
// source for the bookmarklet in templates/bukuserver/home.html:
//
// 1. paste this code in https://bookmarklets.org/maker/
// 2. copy the result to home.html in the bookmarklet anchor href
// 3. Replace "URL_FOR" with "{{url_for("bookmarklet",_external=True)}}"
var url = location.href;
var title = document.title.trim() || "";
var desc = document.getSelection().toString().trim();
if(desc.length > 4000){
desc = desc.substr(0,4000) + '...';
alert('The selected text is too long, it will be truncated.');
}
url = "URL_FOR" +
"?url=" + encodeURIComponent(url) +
"&title=" + encodeURIComponent(title) +
"&description=" + encodeURIComponent(desc);
window.open(url, '_blank', 'menubar=no, height=600, width=600, toolbar=no, scrollbars=yes, status=no, dialog=1');

View File

@ -279,6 +279,8 @@ def create_app(db_file=None):
view_func=bookmark_range_api_view, methods=['GET', 'PUT', 'DELETE'])
bookmark_search_api_view = ApiBookmarkSearchView.as_view('bookmark_search_api')
app.add_url_rule('/api/bookmarks/search', view_func=bookmark_search_api_view, methods=['GET', 'DELETE'])
bookmarklet_view = BookmarkletView.as_view('bookmarklet')
app.add_url_rule('/bookmarklet', view_func=bookmarklet_view, methods=['GET'])
# non api
admin.add_view(views.BookmarkModelView(
bukudb, 'Bookmarks', page_size=per_page, url_render_mode=url_render_mode))
@ -542,6 +544,19 @@ class ApiBookmarkSearchView(MethodView):
return res
class BookmarkletView(MethodView):
def get(self):
url = request.args.get('url')
title = request.args.get('title')
description = request.args.get('description')
bukudb = getattr(flask.g, 'bukudb', get_bukudb())
rec_id = bukudb.get_rec_id(url)
if rec_id >= 0:
return redirect(url_for('bookmark.edit_view', id=rec_id))
return redirect(url_for('bookmark.create_view', url=url, title=title, description=description))
class CustomFlaskGroup(FlaskGroup): # pylint: disable=too-few-public-methods
def __init__(self, **kwargs):
super().__init__(**kwargs)

View File

@ -34,6 +34,14 @@
<button type="submit" class="btn btn-default">Search</button>
</form>
</div>
<div class=" col-md-4 col-md-offset-4">
<p style="padding: 2em"> Bookmarklet:
<a title="Drag this link to your bookmarks toolbar"
href="javascript:void%20function(){var%20e=location.href,t=document.title.trim()||%22%22,o=document.getSelection().toString().trim();o.length%3E4e3%26%26(o=o.substr(0,4e3)+%22...%22,alert(%22The%20selected%20text%20is%20too%20long,%20it%20will%20be%20truncated.%22)),e=%22{{url_for("bookmarklet",_external=True)}}%3Furl=%22+encodeURIComponent(e)+%22%26title=%22+encodeURIComponent(t)+%22%26description=%22+encodeURIComponent(o),window.open(e,%22_blank%22,%22menubar=no,%20height=600,%20width=600,%20toolbar=no,%20scrollbars=yes,%20status=no,%20dialog=1%22)}();">
<b>✚ Add to Buku</b>
</a>
</p>
</div>
</div>
</div>
{% endblock %}

View File

@ -152,6 +152,16 @@ class BookmarkModelView(BaseModelView):
self.url_render_mode = kwargs.pop('url_render_mode', DEFAULT_URL_RENDER_MODE)
super().__init__(*args, **kwargs)
def create_form(self, obj=None):
form = super().create_form(obj)
if 'url' in request.args.keys():
form.url.data = request.args.get("url")
if 'title' in request.args.keys():
form.title.data = request.args.get("title")
if 'description' in request.args.keys():
form.description.data = request.args.get("description")
return form
def create_model(self, form):
try:
model = SimpleNamespace(id=None, url=None, title=None, tags=None, description=None)