Register protocol handler

close #11
This commit is contained in:
CzBiX
2020-03-31 22:46:42 +08:00
parent 82e66e38af
commit 707400b7be
3 changed files with 45 additions and 1 deletions

View File

@@ -74,6 +74,8 @@
<script lang="ts">
import Vue from 'vue';
import { mapGetters, mapState, mapMutations } from 'vuex';
import { registerProtocolHandler, checkDownloadUrl } from './protocolHandler';
import GlobalDialog from './components/GlobalDialog.vue';
import GlobalSnackBar from './components/GlobalSnackBar.vue';
@@ -145,7 +147,20 @@ export default class App extends Vue {
return this.$vuetify.breakpoint.xsOnly;
}
initProtocolHandler() {
registerProtocolHandler();
const url = checkDownloadUrl();
if (url) {
this.setPasteUrl({
url,
});
}
}
async created() {
this.initProtocolHandler();
await this.getInitData();
appWrapEl = (this.$refs.app as any).$el.querySelector('.v-application--wrap');
appWrapEl.addEventListener('paste', this.onPaste);

View File

@@ -317,7 +317,7 @@ export default class AddForm extends Vue {
this.files = files;
}
@Watch('pasteUrl')
@Watch('pasteUrl', {immediate: true})
onPasteUrl(v: string) {
if (!v) {
return;

29
src/protocolHandler.ts Normal file
View File

@@ -0,0 +1,29 @@
function registerProtocolHandler() {
if (!('registerProtocolHandler' in navigator)) {
return;
}
try {
navigator.registerProtocolHandler('magnet', location.origin + '#url=%s', document.title);
} catch (e) {
console.log('Register protocol handler failed.', e);
}
}
function checkDownloadUrl() {
if (!location.hash) {
return null
}
const params = new URLSearchParams(location.hash.substring(1));
const url = params.get('url')
if (!url) {
return null;
}
params.delete('url');
location.hash = '#' + params.toString()
return url
}
export { registerProtocolHandler, checkDownloadUrl };