You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
NewEduCoderBuild/js/jupyter.js

74 lines
1.7 KiB

var lockEvent = false;
; (function (open) {
XMLHttpRequest.prototype.open = function (method, url, async, user, password) {
this.addEventListener("load", function () {
if (method === "PUT") {
// if(!lockEvent)
window.top.postMessage('jupytermessage', '*');
}
});
open.call(this, method, url, async, user, password);
};
})(XMLHttpRequest.prototype.open);
(function (fetch) {
window.fetch = function (url, options) {
options = options || {}
return fetch.apply(this, arguments).then(function (response) {
try {
if (options.method === "PUT" || (typeof url === "object" && url.method === "PUT")) {
// if(!lockEvent)
window.top.postMessage('jupytermessage', '*');
}
} catch (error) {
}
return response;
});
};
})(window.fetch);
function saveCode() {
try {
var elements = document.querySelectorAll('[data-command="docmanager:save"]');
elements.forEach(function (element) {
element.click();
});
} catch (error) {
}
try {
document.querySelector("[data-jupyter-action='jupyter-notebook:save-notebook']").click();
} catch (error) {
}
var saveEvent = new KeyboardEvent('keydown', {
key: 's',
ctrlKey: true
});
document.dispatchEvent(saveEvent);
}
function onReceiveMessage(e) {
try {
if (e.data === 'saveCode') {
lockEvent = true;
saveCode()
setTimeout(() => {
lockEvent = false
}, 1000)
}
} catch (error) {
console.log('error:', error, e);
}
}
window.addEventListener('message', onReceiveMessage);