Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copy JSON to clipboard #367

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions scripts/templates/script.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
// Set the name of the page to the name & version of the script.
document.title = "{{ script.name }} - v{{ script_version.version }}"

function decodeHtml(html) {
var txt = document.createElement("textarea");
txt.innerHTML = html;
return txt.value;
}

function showAndDismissAlert(message) {
var htmlAlert = '<div class="alert alert-primary' + '">' + message + '</div>';

Expand All @@ -29,9 +35,9 @@
$(".alert-messages .alert").first().hide().fadeIn(200).delay(2000).fadeOut(1000, function () { $(this).remove(); });
}

function CopyLink() {
navigator.clipboard.writeText("{{request.scheme}}://{{request.META.HTTP_HOST}}/api/scripts/{{script_version.pk}}/json/")
showAndDismissAlert("Link Copied to Clipboard")
function CopyLink(json) {
navigator.clipboard.writeText(decodeHtml(json))
showAndDismissAlert("JSON Copied to Clipboard")
}
</script>

Expand Down Expand Up @@ -111,7 +117,7 @@ <h4><span class="badge badge-pill badge-dark">You</span></h4>
</div>
{% endif %}
<div class="col-md-auto p-1">
<button class="btn btn-primary" onclick="CopyLink()">JSON</button>
<button class="btn btn-primary" onclick="CopyLink('{{json}}')">JSON</button>
</div>
{% if script_version.edition > 2 %}
{% if perms.scripts.download_unsupported_json %}
Expand Down
2 changes: 2 additions & 0 deletions scripts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from dataclasses import dataclass
from typing import Dict, Any, List, Optional
import requests
import json


class ScriptsListView(SingleTableMixin, FilterView):
Expand Down Expand Up @@ -261,6 +262,7 @@ def get_context_data(self, **kwargs):
)

context["can_delete"] = self.request.user == current_script.script.owner
context["json"] = json.dumps(current_script.content)

return context

Expand Down