From 0f263b89e51a66e6dc0f91edd27ce2db9e9805da Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sat, 19 Mar 2016 17:09:05 -0700 Subject: [PATCH] OS X: open magnet links in WebTorrent --- bin/package.js | 9 +++++++++ main/index.js | 10 +++++----- renderer/index.js | 23 ++++++++++++----------- 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/bin/package.js b/bin/package.js index 3ee0cd2b..72b2df38 100755 --- a/bin/package.js +++ b/bin/package.js @@ -167,6 +167,15 @@ function buildDarwin (cb) { } ] + infoPlist.CFBundleURLTypes = [ + { + CFBundleTypeRole: 'Editor', + CFBundleURLIconFile: 'WebTorrentFile.icns', + CFBundleURLName: 'BitTorrent Magnet URL', + CFBundleURLSchemes: [ 'magnet' ] + } + ] + infoPlist.NSHumanReadableCopyright = 'Copyright © 2014-2016 The WebTorrent Project' fs.writeFileSync(infoPlistPath, plist.build(infoPlist)) diff --git a/main/index.js b/main/index.js index 87038993..20fee9b7 100644 --- a/main/index.js +++ b/main/index.js @@ -40,13 +40,13 @@ ipc.init() function onOpen (e, torrentId) { e.preventDefault() - console.log(app.ipcReady) if (app.ipcReady) { - openFiles() + onReadyOpen() } else { - app.on('ipcReady', openFiles) + app.on('ipcReady', onReadyOpen) } - function openFiles () { - windows.main.send('dispatch', 'openFiles', torrentId) + function onReadyOpen () { + windows.main.send('dispatch', 'onOpen', torrentId) + windows.main.focus() } } diff --git a/renderer/index.js b/renderer/index.js index 704e16de..91d668a6 100644 --- a/renderer/index.js +++ b/renderer/index.js @@ -87,7 +87,7 @@ function init () { Cast.init(update) // ...drag and drop a torrent or video file to play or seed - dragDrop('body', (files) => dispatch('openFiles', files)) + dragDrop('body', (files) => dispatch('onOpen', files)) // ...same thing if you paste a torrent document.addEventListener('paste', onPaste) @@ -157,8 +157,8 @@ function dispatch (action, ...args) { if (['videoMouseMoved', 'playbackJump'].indexOf(action) < 0) { console.log('dispatch: %s %o', action, args) /* log user interactions, but don't spam */ } - if (action === 'openFiles') { - openFiles(args[0] /* files */) + if (action === 'onOpen') { + onOpen(args[0] /* files */) } if (action === 'addTorrent') { addTorrent(args[0] /* torrent */) @@ -324,16 +324,16 @@ function updateClientProgress () { state.dock.progress = progress } -function openFiles (files) { +function onOpen (files) { if (!Array.isArray(files)) files = [ files ] // .torrent file = start downloading the torrent - files.filter(isTorrentFile).forEach(function (torrentFile) { + files.filter(isTorrent).forEach(function (torrentFile) { addTorrent(torrentFile) }) // everything else = seed these files - seed(files.filter(isNotTorrentFile)) + seed(files.filter(isNotTorrent)) } function onPaste (e) { @@ -347,14 +347,15 @@ function onPaste (e) { }) } -function isTorrentFile (file) { +function isTorrent (file) { var name = typeof file === 'string' ? file : file.name - var extname = path.extname(name).toLowerCase() - return extname === '.torrent' + var isTorrentFile = path.extname(name).toLowerCase() === '.torrent' + var isMagnet = typeof file === 'string' && /^magnet:/.test(file) + return isTorrentFile || isMagnet } -function isNotTorrentFile (file) { - return !isTorrentFile(file) +function isNotTorrent (file) { + return !isTorrent(file) } // Gets a torrent summary {name, infoHash, status} from state.saved.torrents