2021-02-21 19:48:08 +03:00
|
|
|
// polyfills
|
|
|
|
if (!String.prototype.startsWith) {
|
|
|
|
String.prototype.startsWith = function (searchString, position) {
|
|
|
|
position = position || 0;
|
|
|
|
return this.substr(position, searchString.length) === searchString;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
(function () {
|
|
|
|
function s4() {
|
|
|
|
return Math.floor((1 + Math.random()) * 0x10000)
|
|
|
|
.toString(16)
|
|
|
|
.substring(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
var uid = function () {
|
|
|
|
return (
|
|
|
|
s4() +
|
|
|
|
s4() +
|
|
|
|
"-" +
|
|
|
|
s4() +
|
|
|
|
"-" +
|
|
|
|
s4() +
|
|
|
|
"-" +
|
|
|
|
s4() +
|
|
|
|
"-" +
|
|
|
|
s4() +
|
|
|
|
s4() +
|
|
|
|
s4()
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
function ownKeys(object, enumerableOnly) {
|
|
|
|
var keys = Object.keys(object);
|
|
|
|
if (Object.getOwnPropertySymbols) {
|
|
|
|
var symbols = Object.getOwnPropertySymbols(object);
|
|
|
|
if (enumerableOnly)
|
|
|
|
symbols = symbols.filter(function (sym) {
|
|
|
|
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
|
|
});
|
|
|
|
keys.push.apply(keys, symbols);
|
|
|
|
}
|
|
|
|
return keys;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _objectSpread(target) {
|
|
|
|
for (var i = 1; i < arguments.length; i++) {
|
|
|
|
var source = arguments[i] != null ? arguments[i] : {};
|
|
|
|
if (i % 2) {
|
|
|
|
ownKeys(source, true).forEach(function (key) {
|
|
|
|
_defineProperty(target, key, source[key]);
|
|
|
|
});
|
|
|
|
} else if (Object.getOwnPropertyDescriptors) {
|
|
|
|
Object.defineProperties(
|
|
|
|
target,
|
|
|
|
Object.getOwnPropertyDescriptors(source)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
ownKeys(source).forEach(function (key) {
|
|
|
|
Object.defineProperty(
|
|
|
|
target,
|
|
|
|
key,
|
|
|
|
Object.getOwnPropertyDescriptor(source, key)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return target;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _defineProperty(obj, key, value) {
|
|
|
|
if (key in obj) {
|
|
|
|
Object.defineProperty(obj, key, {
|
|
|
|
value: value,
|
|
|
|
enumerable: true,
|
|
|
|
configurable: true,
|
|
|
|
writable: true,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
obj[key] = value;
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!window.__TAURI__) {
|
|
|
|
window.__TAURI__ = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
window.__TAURI__.transformCallback = function transformCallback(
|
|
|
|
callback,
|
|
|
|
once
|
|
|
|
) {
|
|
|
|
var identifier = uid();
|
|
|
|
|
|
|
|
window[identifier] = function (result) {
|
|
|
|
if (once) {
|
|
|
|
delete window[identifier];
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback && callback(result);
|
|
|
|
};
|
|
|
|
|
|
|
|
return identifier;
|
|
|
|
};
|
|
|
|
|
2021-03-07 05:19:12 +03:00
|
|
|
window.__TAURI__.invoke = function invoke(cmd, args = {}) {
|
2021-02-21 19:48:08 +03:00
|
|
|
var _this = this;
|
|
|
|
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
var callback = _this.transformCallback(function (r) {
|
|
|
|
resolve(r);
|
|
|
|
delete window[error];
|
|
|
|
}, true);
|
|
|
|
var error = _this.transformCallback(function (e) {
|
|
|
|
reject(e);
|
|
|
|
delete window[callback];
|
|
|
|
}, true);
|
|
|
|
|
2021-03-07 05:19:12 +03:00
|
|
|
if (typeof cmd === "string") {
|
|
|
|
args.cmd = cmd;
|
|
|
|
} else if (typeof cmd === "object") {
|
|
|
|
args = cmd;
|
|
|
|
} else {
|
|
|
|
return reject(new Error("Invalid argument type."));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (window.rpc) {
|
|
|
|
window.rpc.notify(
|
|
|
|
cmd,
|
2021-02-24 06:43:21 +03:00
|
|
|
_objectSpread(
|
|
|
|
{
|
|
|
|
callback: callback,
|
|
|
|
error: error,
|
|
|
|
},
|
|
|
|
args
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
window.addEventListener("DOMContentLoaded", function () {
|
2021-03-07 05:19:12 +03:00
|
|
|
window.rpc.notify(
|
|
|
|
cmd,
|
2021-02-21 19:48:08 +03:00
|
|
|
_objectSpread(
|
|
|
|
{
|
|
|
|
callback: callback,
|
|
|
|
error: error,
|
|
|
|
},
|
|
|
|
args
|
|
|
|
)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// open <a href="..."> links with the Tauri API
|
|
|
|
function __openLinks() {
|
|
|
|
document.querySelector("body").addEventListener(
|
|
|
|
"click",
|
|
|
|
function (e) {
|
|
|
|
var target = e.target;
|
|
|
|
while (target != null) {
|
|
|
|
if (
|
|
|
|
target.matches ? target.matches("a") : target.msMatchesSelector("a")
|
|
|
|
) {
|
|
|
|
if (
|
|
|
|
target.href &&
|
|
|
|
target.href.startsWith("http") &&
|
|
|
|
target.target === "_blank"
|
|
|
|
) {
|
2021-03-07 05:19:12 +03:00
|
|
|
window.__TAURI__.invoke('tauri', {
|
2021-02-21 19:48:08 +03:00
|
|
|
__tauriModule: "Shell",
|
|
|
|
message: {
|
|
|
|
cmd: "open",
|
|
|
|
uri: target.href,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
target = target.parentElement;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
document.readyState === "complete" ||
|
|
|
|
document.readyState === "interactive"
|
|
|
|
) {
|
|
|
|
__openLinks();
|
|
|
|
} else {
|
|
|
|
window.addEventListener(
|
|
|
|
"DOMContentLoaded",
|
|
|
|
function () {
|
|
|
|
__openLinks();
|
|
|
|
},
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-07 05:19:12 +03:00
|
|
|
window.__TAURI__.invoke('tauri', {
|
|
|
|
__tauriModule: "Event",
|
2021-02-21 19:48:08 +03:00
|
|
|
message: {
|
2021-03-07 05:19:12 +03:00
|
|
|
cmd: "listen",
|
|
|
|
event: "tauri://window-created",
|
2021-02-21 19:48:08 +03:00
|
|
|
handler: window.__TAURI__.transformCallback(function (event) {
|
|
|
|
if (event.payload) {
|
2021-03-07 05:19:12 +03:00
|
|
|
var windowLabel = event.payload.label;
|
|
|
|
window.__TAURI__.__windows.push({ label: windowLabel });
|
2021-02-21 19:48:08 +03:00
|
|
|
}
|
2021-03-07 05:19:12 +03:00
|
|
|
}),
|
|
|
|
},
|
|
|
|
});
|
2021-02-21 19:48:08 +03:00
|
|
|
|
|
|
|
let permissionSettable = false;
|
|
|
|
let permissionValue = "default";
|
|
|
|
|
|
|
|
function isPermissionGranted() {
|
|
|
|
if (window.Notification.permission !== "default") {
|
|
|
|
return Promise.resolve(window.Notification.permission === "granted");
|
|
|
|
}
|
2021-03-07 05:19:12 +03:00
|
|
|
return window.__TAURI__.invoke('tauri', {
|
2021-02-21 19:48:08 +03:00
|
|
|
__tauriModule: "Notification",
|
|
|
|
message: {
|
|
|
|
cmd: "isNotificationPermissionGranted",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function setNotificationPermission(value) {
|
|
|
|
permissionSettable = true;
|
|
|
|
window.Notification.permission = value;
|
|
|
|
permissionSettable = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function requestPermission() {
|
|
|
|
return window.__TAURI__
|
2021-03-07 05:19:12 +03:00
|
|
|
.invoke('tauri', {
|
2021-02-21 19:48:08 +03:00
|
|
|
__tauriModule: "Notification",
|
|
|
|
mainThread: true,
|
|
|
|
message: {
|
|
|
|
cmd: "requestNotificationPermission",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.then(function (permission) {
|
|
|
|
setNotificationPermission(permission);
|
|
|
|
return permission;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function sendNotification(options) {
|
|
|
|
if (typeof options === "object") {
|
|
|
|
Object.freeze(options);
|
|
|
|
}
|
|
|
|
|
|
|
|
isPermissionGranted().then(function (permission) {
|
|
|
|
if (permission) {
|
2021-03-07 05:19:12 +03:00
|
|
|
return window.__TAURI__.invoke('tauri', {
|
2021-02-21 19:48:08 +03:00
|
|
|
__tauriModule: "Notification",
|
|
|
|
message: {
|
|
|
|
cmd: "notification",
|
|
|
|
options:
|
|
|
|
typeof options === "string"
|
|
|
|
? {
|
|
|
|
title: options,
|
|
|
|
}
|
|
|
|
: options,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
window.Notification = function (title, options) {
|
|
|
|
var opts = options || {};
|
|
|
|
sendNotification(
|
|
|
|
Object.assign(opts, {
|
|
|
|
title: title,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
window.Notification.requestPermission = requestPermission;
|
|
|
|
|
|
|
|
Object.defineProperty(window.Notification, "permission", {
|
|
|
|
enumerable: true,
|
|
|
|
get: function () {
|
|
|
|
return permissionValue;
|
|
|
|
},
|
|
|
|
set: function (v) {
|
|
|
|
if (!permissionSettable) {
|
|
|
|
throw new Error("Readonly property");
|
|
|
|
}
|
|
|
|
permissionValue = v;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
isPermissionGranted().then(function (response) {
|
|
|
|
if (response === null) {
|
|
|
|
setNotificationPermission("default");
|
|
|
|
} else {
|
|
|
|
setNotificationPermission(response ? "granted" : "denied");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
window.alert = function (message) {
|
2021-03-07 05:19:12 +03:00
|
|
|
window.__TAURI__.invoke('tauri', {
|
2021-02-21 19:48:08 +03:00
|
|
|
__tauriModule: "Dialog",
|
|
|
|
mainThread: true,
|
|
|
|
message: {
|
|
|
|
cmd: "messageDialog",
|
|
|
|
message: message,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
window.confirm = function (message) {
|
2021-03-07 05:19:12 +03:00
|
|
|
return window.__TAURI__.invoke('tauri', {
|
2021-02-21 19:48:08 +03:00
|
|
|
__tauriModule: "Dialog",
|
|
|
|
mainThread: true,
|
|
|
|
message: {
|
|
|
|
cmd: "askDialog",
|
|
|
|
message: message,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
})();
|