53 lines
1.5 KiB
JavaScript
53 lines
1.5 KiB
JavaScript
'use strict';
|
|
|
|
function onMountRequested(onSuccess, onError) {
|
|
console.log("mount requested");
|
|
getMountInfo(function(mountInfo) { mount(mountInfo, onSuccess, onError); });
|
|
}
|
|
|
|
|
|
function getMountInfo(callback) {
|
|
let windowCallback = function(appWindow) {
|
|
let listener = function(message) {
|
|
if(message.senderId == appWindow.id) {
|
|
callback(message.mountInfo);
|
|
} else {
|
|
console.log(message);
|
|
console.log(appWindow);
|
|
console.log(message.senderId);
|
|
console.log(appWindow.id);
|
|
}
|
|
chrome.runtime.onMessage.removeListener(listener);
|
|
};
|
|
chrome.runtime.onMessage.addListener(listener);
|
|
};
|
|
|
|
let windowOptions = { outerBounds : { width: 800, height: 600 }, resizable : false, id: Date.now().toString() };
|
|
chrome.app.window.create("mount-info.html", windowOptions, windowCallback);
|
|
}
|
|
|
|
function mount(mountInfo, onSuccess, onError) {
|
|
console.log("mount()");
|
|
let request = new XMLHttpRequest();
|
|
request.open("GET", mountInfo.url, true);
|
|
request.onreadystatechange = function() {
|
|
if(request.readyState != XMLHttpRequest.DONE) return;
|
|
try {
|
|
JSON.parse(request.responseText);
|
|
|
|
let options = { fileSystemId: mountInfo.url, displayName: mountInfo.name, writable: false };
|
|
chrome.fileSystemProvider.mount(options);
|
|
|
|
console.log("mount succeeded");
|
|
onSuccess();
|
|
} catch(exception) {
|
|
console.log(exception);
|
|
console.log("mount failed");
|
|
onError();
|
|
}
|
|
};
|
|
request.send();
|
|
}
|
|
|
|
chrome.fileSystemProvider.onMountRequested.addListener(onMountRequested);
|