Skip to content

A full-featured download manager.

feat: handle magnet protocol

Dr_rOotefcf01d59b69
diff --git a/package.json b/package.json
index 0d91a8c6..ca493044 100644
--- a/package.json
+++ b/package.json
@@ -47,6 +47,21 @@
"files": [
"dist/electron/**/*"
],
+ "protocols": [
+ {
+ "name": "Motrix Protocol",
+ "schemes": [
+ "mo",
+ "motrix"
+ ]
+ },
+ {
+ "name": "Magnet Protocol",
+ "schemes": [
+ "magnet"
+ ]
+ }
+ ],
"dmg": {
"window": {
"width": 540,
@@ -83,16 +98,7 @@
"binaries": [
"./release/mac/Motrix.app/Contents/Resources/engine/aria2c"
],
- "category": "public.app-category.utilities",
- "protocols": [
- {
- "name": "Motrix Protocol",
- "schemes": [
- "mo",
- "motrix"
- ]
- }
- ]
+ "category": "public.app-category.utilities"
},
"win": {
"target": [
diff --git a/src/main/core/ProtocolManager.js b/src/main/core/ProtocolManager.js
index 034d484f..be132520 100644
--- a/src/main/core/ProtocolManager.js
+++ b/src/main/core/ProtocolManager.js
@@ -13,17 +13,43 @@ export default class ProtocolManager extends EventEmitter {
}
init () {
- // package.json:build.mac.protocols[].schemes[]
+ // package.json:build.protocols[].schemes[]
if (!app.isDefaultProtocolClient('mo')) {
app.setAsDefaultProtocolClient('mo')
}
if (!app.isDefaultProtocolClient('motrix')) {
app.setAsDefaultProtocolClient('motrix')
}
+ if (!app.isDefaultProtocolClient('magnet')) {
+ app.setAsDefaultProtocolClient('magnet')
+ }
}
handle (url) {
logger.info(`[Motrix] protocol url: ${url}`)
+
+ if (url.toLowerCase().startsWith('magnet:')) {
+ return this.handleMagnetProtocol(url)
+ }
+
+ if (
+ url.toLowerCase().startsWith('mo:') ||
+ url.toLowerCase().startsWith('motrix:')
+ ) {
+ return this.handleMoProtocol(url)
+ }
+ }
+
+ handleMagnetProtocol (url) {
+ if (!url) {
+ return
+ }
+ logger.error(`[Motrix] handleMagnetProtocol url: ${url}`)
+
+ global.application.sendCommandToAll('application:new-task', 'uri', url)
+ }
+
+ handleMoProtocol (url) {
const parsed = new URL(url)
const { host } = parsed
logger.info('[Motrix] protocol parsed:', parsed, host)
diff --git a/src/renderer/components/Command/index.js b/src/renderer/components/Command/index.js
index fafc27ac..41f2a7bb 100644
--- a/src/renderer/components/Command/index.js
+++ b/src/renderer/components/Command/index.js
@@ -21,7 +21,10 @@ function showAboutPanel () {
store.dispatch('app/showAboutPanel')
}
-function showAddTask (taskType = 'uri') {
+function showAddTask (taskType = 'uri', task = '') {
+ if (taskType === 'uri' && task) {
+ store.dispatch('app/updateAddTaskUrl', task)
+ }
store.dispatch('app/showAddTaskDialog', taskType)
}
diff --git a/src/renderer/components/Task/AddTask.vue b/src/renderer/components/Task/AddTask.vue
index 10d03072..fee2c4a2 100644
--- a/src/renderer/components/Task/AddTask.vue
+++ b/src/renderer/components/Task/AddTask.vue
@@ -123,9 +123,10 @@
} from '@shared/utils'
const initialForm = (state) => {
+ const { addTaskUrl } = state.app
const { dir, split, newTaskShowDownloading } = state.preference.config
const result = {
- uris: '',
+ uris: addTaskUrl,
torrent: '',
out: '',
userAgent: '',
@@ -211,7 +212,9 @@
if (!hasResource) {
return
}
- this.form.uris = content
+ if (isEmpty(this.form.uris)) {
+ this.form.uris = content
+ }
},
handleClose (done) {
this.$store.dispatch('app/hideAddTaskDialog')
diff --git a/src/renderer/store/modules/app.js b/src/renderer/store/modules/app.js
index def6214d..a7e648b0 100644
--- a/src/renderer/store/modules/app.js
+++ b/src/renderer/store/modules/app.js
@@ -25,6 +25,7 @@ const state = {
},
addTaskVisible: false,
addTaskType: 'uri',
+ addTaskUrl: '',
addTaskTorrents: []
}
@@ -53,6 +54,9 @@ const mutations = {
CHANGE_ADD_TASK_TYPE (state, taskType) {
state.addTaskType = taskType
},
+ CHANGE_ADD_TASK_URL (state, text) {
+ state.addTaskUrl = text
+ },
CHANGE_ADD_TASK_TORRENTS (state, fileList) {
state.addTaskTorrents = [...fileList]
},
@@ -130,7 +134,7 @@ const actions = {
}
})
},
- togglePowerSaveBlocker (context, numActive) {
+ togglePowerSaveBlocker (_, numActive) {
if (numActive > 0) {
api.startPowerSaveBlocker()
} else {
@@ -146,11 +150,15 @@ const actions = {
},
hideAddTaskDialog ({ commit }) {
commit('CHANGE_ADD_TASK_VISIBLE', false)
+ commit('CHANGE_ADD_TASK_URL', '')
commit('CHANGE_ADD_TASK_TORRENTS', [])
},
changeAddTaskType ({ commit }, taskType) {
commit('CHANGE_ADD_TASK_TYPE', taskType)
},
+ updateAddTaskUrl ({ commit }, text = '') {
+ commit('CHANGE_ADD_TASK_URL', text)
+ },
addTaskAddTorrents ({ commit }, { fileList }) {
commit('CHANGE_ADD_TASK_TORRENTS', fileList)
},