added bookmarklet that doesn't work yet

This commit is contained in:
smilerz 2021-03-25 14:02:57 -05:00
parent 4d5a9e446f
commit 09d2e9f831
4 changed files with 69 additions and 3 deletions

View File

@ -0,0 +1,37 @@
(function(){
var v = "1.3.2";
if (window.jQuery === undefined || window.jQuery.fn.jquery < v) {
var done = false;
var script = document.createElement("script");
script.src = "https://ajax.googleapis.com/ajax/libs/jquery/" + v + "/jquery.min.js";
script.onload = script.onreadystatechange = function(){
if (!done && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete")) {
done = true;
initBookmarklet();
}
};
document.getElementsByTagName("head")[0].appendChild(script);
} else {
initBookmarklet();
}
function initBookmarklet() {
(window.bookmarkletTandoor = function() {
r = confirm('Click OK to import recipe automatically, click Cancel to import the recipe manually.');
if (r) {
auto=true
} else {
auto=false
}
var newIframe = document.createElement('iframe');
newIframe.width = '200';newIframe.height = '200';
newIframe.src = localStorage.getItem('importURL');
document.body.appendChild(newIframe);
}
)();
}
})();

View File

@ -1,6 +1,7 @@
{% extends "base.html" %}
{% load i18n %}
{% load static %}
{% load custom_tags %}
{% block title %}{% trans 'URL Import' %}{% endblock %}
@ -20,9 +21,15 @@
{% endblock %}
{% block content %}
<div id="app">
<h2> {% trans 'Import' %} </h2>
<div class="row px-3" style="justify-content:space-between;">
<h2> {% trans 'Import' %} </h2> <!--{{ request.get_host }}{% url 'data_import_url' %} -->
<a class="btn btn-outline-info btn-sm"
style="height:50%"
href="{% bookmarklet request.get_host request.is_secure %}"
title="{% trans 'Drag me to your bookmarks to import recipes from anywhere' %}">
<img src="{% static 'assets/favicon-16x16.png' %}">{% trans 'Bookmark Me!' %} </a>
</div>
<nav class="nav nav-pills flex-sm-row" style="margin-bottom:10px">
<a class="nav-link active" href="#nav-url" data-toggle="tab" role="tab" aria-controls="nav-url" aria-selected="true">URL</a>
<a class="nav-link" href="#nav-app" data-toggle="tab" role="tab" aria-controls="nav-app">App</a>

View File

@ -1,4 +1,5 @@
import bleach
import re
import markdown as md
from bleach_allowlist import markdown_attrs, markdown_tags
from cookbook.helper.mdx_attributes import MarkdownFormatExtension
@ -6,6 +7,7 @@ from cookbook.helper.mdx_urlize import UrlizeExtension
from cookbook.models import Space, get_model_name
from django import template
from django.db.models import Avg
from django.templatetags.static import static
from django.urls import NoReverseMatch, reverse
from recipes import settings
from gettext import gettext as _
@ -106,3 +108,23 @@ def message_of_the_day():
@register.simple_tag
def is_debug():
return settings.DEBUG
@register.simple_tag
def bookmarklet(host, secure):
if secure:
prefix = "https://"
else:
prefix = "http://"
bookmark = "javascript: \
(function(){ \
if(window.bookmarkletTandoor!==undefined){ \
bookmarkletTandoor(); \
} else { \
localStorage.setItem('importURL', '"+ prefix + host + reverse('api_bookmarklet') +"'); \
document.body.appendChild(document.createElement(\'script\')).src=\'" \
+ prefix + host + static('js/bookmarklet.js') + "? \
r=\'+Math.floor(Math.random()*999999999);}})();"
return re.sub(r"[\n\t]*", "", bookmark)

View File

@ -92,7 +92,7 @@ urlpatterns = [
path('api/sync_all/', api.sync_all, name='api_sync'),
path('api/log_cooking/<int:recipe_id>/', api.log_cooking, name='api_log_cooking'),
path('api/plan-ical/<slug:from_date>/<slug:to_date>/', api.get_plan_ical, name='api_get_plan_ical'),
path('api/recipe-from-url/', api.recipe_from_url, name='api_recipe_from_url'),
path('api/recipe-from-url/', api.recipe_from_url, name='api_recipe_from_url'),
path('api/recipe-from-source/', api.recipe_from_source, name='api_recipe_from_source'),
path('api/backup/', api.get_backup, name='api_backup'),
path('api/ingredient-from-string/', api.ingredient_from_string, name='api_ingredient_from_string'),