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.
124 lines
2.6 KiB
124 lines
2.6 KiB
var lockEvent = false;
|
|
; (function (open) {
|
|
XMLHttpRequest.prototype.open = function (method, url, async, user, password) {
|
|
this.addEventListener("load", function () {
|
|
if (method === "PUT") {
|
|
// if(!lockEvent)
|
|
try {
|
|
if (url.indexOf("/api/workspaces/default") > -1) {
|
|
return
|
|
}
|
|
} catch (error) {
|
|
|
|
};
|
|
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)
|
|
try {
|
|
if (url.url.indexOf("/api/workspaces/default") > -1) {
|
|
return
|
|
}
|
|
} catch (error) {
|
|
|
|
};
|
|
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);
|
|
|
|
|
|
|
|
function getJupyterPath() {
|
|
var el = document.querySelector('.jp-mod-running');
|
|
if (!el) return;
|
|
|
|
var title = el.getAttribute('title') || '';
|
|
|
|
var lines = title.split('\n');
|
|
|
|
var name = null;
|
|
var path = null;
|
|
|
|
for (var i = 0; i < lines.length; i++) {
|
|
var line = lines[i].trim();
|
|
|
|
if (line.indexOf('Name:') === 0) {
|
|
name = line.replace('Name:', '').trim();
|
|
}
|
|
|
|
if (line.indexOf('Path:') === 0) {
|
|
path = line.replace('Path:', '').trim();
|
|
}
|
|
}
|
|
|
|
var fullPath = null;
|
|
if (path && name) {
|
|
fullPath = path.replace(/\/$/, '') + '/' + name;
|
|
}
|
|
|
|
console.log(fullPath);
|
|
|
|
sessionStorage.jupyterOpenPath = fullPath
|
|
}
|
|
setInterval(() => {getJupyterPath()},1000)
|