Skip to content

Commit 7f0884e

Browse files
committed
add a template for the pylti1p3 cookie check
1 parent d229074 commit 7f0884e

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
{% extends "numbas_lti/base.html" %}
2+
{% load i18n %}
3+
4+
{% block title %}{% translate "Checking for cookie access" %} - {{block.super}}{% endblock title %}
5+
6+
{% block stylesheets %}
7+
{{block.super}}
8+
<style>
9+
body:not([data-lti1p3-status="success"]) #lti1p3-success-msg,
10+
body:not([data-lti1p3-status="failure"]) #lti1p3-failure-msg,
11+
body:not([data-lti1p3-status="opened"]) #lti1p3-opened-msg {
12+
display: none;
13+
}
14+
</style>
15+
{% endblock stylesheets %}
16+
17+
{% block javascripts %}
18+
{{params|json_script:"urlParams"}}
19+
20+
<script type="text/javascript" defer>
21+
const urlParams = JSON.parse(document.getElementById('urlParams').textContent)
22+
var siteProtocol = '{{protocol}}';
23+
var htmlEntities = {
24+
"&lt;": "<",
25+
"&gt;": ">",
26+
"&amp;": "&",
27+
"&quot;": '"',
28+
"&#x27;": "'"
29+
};
30+
31+
function getUpdatedUrl() {
32+
const url = new URL(window.location);
33+
for (var key in urlParams) {
34+
url.searchParams.set(key, urlParams[key]);
35+
}
36+
return url.toString();
37+
}
38+
39+
function set_status(status) {
40+
document.body.dataset.lti1p3Status = status;
41+
}
42+
43+
function checkCookiesAllowed() {
44+
var cookie = "lti1p3_test_cookie=1; path=/";
45+
if (siteProtocol === 'https') {
46+
cookie = cookie + '; SameSite=None; secure';
47+
}
48+
document.cookie = cookie;
49+
var cookie_set = document.cookie.indexOf("lti1p3_test_cookie") !== -1;
50+
51+
if (cookie_set) {
52+
set_status('success');
53+
document.cookie = "lti1p3_test_cookie=1; expires=Thu, 01-Jan-1970 00:00:01 GMT";
54+
window.location.href = getUpdatedUrl();
55+
} else {
56+
set_status('failure');
57+
}
58+
}
59+
60+
var newTabLink = document.getElementById("lti1p3-new-tab-link");
61+
newTabLink.href = getUpdatedUrl();
62+
newTabLink.onclick = function() {
63+
set_status('opened');
64+
};
65+
66+
checkCookiesAllowed();
67+
</script>
68+
{% endblock javascripts %}
69+
70+
{% block content %}
71+
<header>
72+
<h1>{% translate "Checking for cookie access" %}</h1>
73+
</header>
74+
<main>
75+
<div id="lti1p3-success-msg">
76+
{% translate "Your browser can set cookies here. Loading the content..." %}
77+
</div>
78+
<div id="lti1p3-failure-msg">
79+
<p>{% translate "Your browser does not allow cookies to be set in this page." %}</p>
80+
<p><a target="_blank" id="lti1p3-new-tab-link">{% translate "Click here to try opening the content in a new tab." %}</a></p>
81+
</div>
82+
<div id="lti1p3-opened-msg">
83+
<p>{% translate "The content has been opened in a new tab." %}</p>
84+
<p>{% translate "You can now close this page." %}</p>
85+
</div>
86+
</main>
87+
{% endblock content %}

0 commit comments

Comments
 (0)