|
7 | 7 | * the service is launched automatically on the next page load. |
8 | 8 | * However, this is disabled if the destination is a blank page. |
9 | 9 | */ |
10 | | -(function() { |
| 10 | +(function($) { |
11 | 11 | 'use strict'; |
12 | 12 |
|
13 | 13 | function json_parse(data) { |
|
28 | 28 | }; |
29 | 29 |
|
30 | 30 | function connect_form_automation(name="<unknown>") { |
31 | | - const element = this; |
32 | | - if (element.dataset[settings.ext_service_connected] === "true") return; |
33 | | - element.dataset[settings.ext_service_connected] = "true"; |
34 | | - const ext_serv = element.dataset[settings.ext_serv_key]; |
35 | | - const ext_title = element.dataset[settings.ext_serv_title]; |
36 | | - const params_hash = element.dataset[settings.ext_params_hash_key]; |
| 31 | + const element = $(this); |
| 32 | + if (element.data(settings.ext_service_connected) === "true") return; |
| 33 | + element.data(settings.ext_service_connected, "true"); |
| 34 | + const ext_serv = element.data(settings.ext_serv_key); |
| 35 | + const ext_title = element.data(settings.ext_serv_title); |
| 36 | + const params_hash = element.data(settings.ext_params_hash_key); |
37 | 37 | if (!ext_serv) { |
38 | 38 | console.log("Invalid external service element:"); |
39 | 39 | console.dir(this); |
|
43 | 43 | } |
44 | 44 | const storage = window.localStorage; |
45 | 45 | const storage_key = "external_service_" + ext_serv; |
46 | | - const form = element.querySelector('form'); |
47 | | - const auto_accept_box = element.querySelector(settings.auto_accept_selector); |
48 | | - const auto_accept = auto_accept_box.querySelector('input[type=checkbox]'); |
49 | | - const target = form.getAttribute('target'); |
| 46 | + const form = element.find('form'); |
| 47 | + const auto_accept_box = element.find(settings.auto_accept_selector); |
| 48 | + const auto_accept = auto_accept_box.find('input[type=checkbox]'); |
| 49 | + const target = form.attr('target'); |
50 | 50 |
|
51 | | - form.addEventListener("submit", function() { |
| 51 | + form.on("submit", function() { |
52 | 52 | if (target && target.charAt(0) !== '_') { |
53 | | - const iframe = element.querySelector("iframe[name='" + target + "']"); |
| 53 | + const iframe = element.find("iframe[name='" + target + "']"); |
54 | 54 | /* show the iframe if the external service is opened in one */ |
55 | | - iframe.style.display = 'block'; |
| 55 | + iframe.show(); |
56 | 56 |
|
57 | 57 | /* hide the warning message and the form */ |
58 | | - element.querySelector(settings.message_area_selector).style.display = 'none'; |
| 58 | + element.find(settings.message_area_selector).hide(); |
59 | 59 | } |
60 | 60 | /* remember the accepted state via a local storage */ |
61 | | - if (auto_accept.checked) { |
| 61 | + if (auto_accept.prop('checked')) { |
62 | 62 | const local_value = json_parse(storage.getItem(storage_key)) || |
63 | 63 | {id: ext_serv, title: ext_title, ok: []}; |
64 | 64 | if (local_value.ok.indexOf(params_hash) < 0) { |
|
85 | 85 | if (target === "_blank") { |
86 | 86 | /* hide the automatic accept checkbox when the automatic launch |
87 | 87 | is disabled due to pop-up blockers */ |
88 | | - auto_accept_box.style.display = 'none'; |
| 88 | + auto_accept_box.hide(); |
89 | 89 | } else if (local_value !== null && |
90 | 90 | local_value.ok.indexOf(params_hash) >= 0) { |
91 | 91 | /* automatically submit data, if form with current data, |
|
95 | 95 | } |
96 | 96 |
|
97 | 97 | function find_and_connect(dom, name) { |
98 | | - dom.querySelectorAll('div.external-service').forEach(function(element) { |
99 | | - connect_form_automation.call(element, name); |
| 98 | + $(dom).find('div.external-service').each(function() { |
| 99 | + connect_form_automation.call(this, name); |
100 | 100 | }); |
101 | 101 | } |
102 | 102 |
|
103 | | - document.addEventListener('aplus:exercise-ready', function(e) { |
| 103 | + $(document).on('aplus:exercise-ready', function(e) { |
104 | 104 | const exercise = e.target; |
105 | 105 | const name = exercise.dataset.aplusExercise || exercise.dataset.aplusChapter || "<unknown exercise>"; |
106 | 106 | find_and_connect(exercise, name); |
107 | 107 | }); |
108 | | - |
109 | | - document.addEventListener('DOMContentLoaded', function() { |
| 108 | + $(function() { |
110 | 109 | find_and_connect(document, "<redirect-page>"); |
111 | 110 | }); |
112 | | -})(); |
| 111 | +})(jQuery); |
0 commit comments