2016-07-19 79 views
3

我在index.js中做了以下操作;電子網址方案「open-url」事件

electron.remote.app.on("open-url", function(event, url) { 
    console.log("Open URL: " + url); 
}); 

這會在Mac OS中觸發,但不在Windows中觸發。是否有另一個事件或不同的方式在Windows中做到這一點?

回答

3

這是一個僅限mac的功能。 最接近的替代方案是app.makeSingleInstance(callback)

你可以安排你的應用的URL作爲參數來啓動:對myApp我的方案://東西

然後callback被稱爲在任何應用程序首次推出的URL。

1

From mine similar Q/A at SO: 這是關於開放的應用程序,並通過使用電子兩個平臺(MACOS/win32)中深層鏈接參數。在MACOS/Win32平臺深度鏈接功能(單實例應用程序)'electron-deep-linking-mac-win' on GitHub


最小的電子項目。


package.json

{ 
    "name": "electron-deeplinking-macos-win32", 
    "version": "0.0.1", 
    "description": "Minimal Electron application with deep inking (macOS/win32)", 
    "main": "main.js", 
    "scripts": { 
    "start": "electron .", 
    "pack": "build --dir", 
    "dist": "build" 
    }, 
    "repository": "https://github.com/oikonomopo/electron-deep-linking-osx", 
    "author": "oikonomopo", 
    "license": "CC0-1.0", 
    "devDependencies": { 
    "electron": "1.6.6", 
    "electron-builder": "17.1.2" 
    }, 
    "build": { 
    "appId": "oikonomopo.electron-deeplinking-macos-win32", 
    "protocols": { 
     "name": "electron-deep-linking", 
     "schemes": ["myapp"] 
    }, 
    "mac": { 
     "category": "public.app-category.Reference" 
    }, 
    "win": { 
    } 
    } 
} 

main.js

const {app, BrowserWindow} = require('electron') 
// Module with utilities for working with file and directory paths. 
const path = require('path') 
// Module with utilities for URL resolution and parsing. 
const url = require('url') 

// Keep a global reference of the window object, if you don't, the window will 
// be closed automatically when the JavaScript object is garbage collected. 
let mainWindow 

// Deep linked url 
let deeplinkingUrl 

// Force Single Instance Application 
const shouldQuit = app.makeSingleInstance((argv, workingDirectory) => { 
    // Someone tried to run a second instance, we should focus our window. 

    // Protocol handler for win32 
    // argv: An array of the second instance’s (command line/deep linked) arguments 
    if (process.platform == 'win32') { 
    // Keep only command line/deep linked arguments 
    deeplinkingUrl = argv.slice(1) 
    } 
    logEverywhere("app.makeSingleInstance# " + deeplinkingUrl) 

    if (win) { 
    if (win.isMinimized()) win.restore() 
     win.focus() 
    } 
}) 
if (shouldQuit) { 
    app.quit() 
    return 
} 

function createWindow() { 
    // Create the browser window. 
    mainWindow = new BrowserWindow({width: 800, height: 600}) 

    // and load the index.html of the app. 
    mainWindow.loadURL(url.format({ 
    pathname: path.join(__dirname, 'index.html'), 
    protocol: 'file:', 
    slashes: true 
    })) 

    // Open the DevTools. 
    mainWindow.webContents.openDevTools() 

    // Protocol handler for win32 
    if (process.platform == 'win32') { 
    // Keep only command line/deep linked arguments 
    deeplinkingUrl = process.argv.slice(1) 
    } 
    logEverywhere("createWindow# " + deeplinkingUrl) 

    // Emitted when the window is closed. 
    mainWindow.on('closed', function() { 
    // Dereference the window object, usually you would store windows 
    // in an array if your app supports multi windows, this is the time 
    // when you should delete the corresponding element. 
    mainWindow = null 
    }) 
} 

// This method will be called when Electron has finished 
// initialization and is ready to create browser windows. 
// Some APIs can only be used after this event occurs. 
app.on('ready', createWindow) 

// Quit when all windows are closed. 
app.on('window-all-closed', function() { 
    // On OS X it is common for applications and their menu bar 
    // to stay active until the user quits explicitly with Cmd + Q 
    if (process.platform !== 'darwin') { 
    app.quit() 
    } 
}) 

app.on('activate', function() { 
    // On OS X it's common to re-create a window in the app when the 
    // dock icon is clicked and there are no other windows open. 
    if (mainWindow === null) { 
    createWindow() 
    } 
}) 

// Define custom protocol handler. Deep linking works on packaged versions of the application! 
app.setAsDefaultProtocolClient('myapp') 

// Protocol handler for osx 
app.on('open-url', function (event, url) { 
    event.preventDefault() 
    deeplinkingUrl = url 
    logEverywhere("open-url# " + deeplinkingUrl) 
}) 

// Log both at dev console and at running node console instance 
function logEverywhere(s) { 
    console.log(s) 
    if (mainWindow && mainWindow.webContents) { 
     mainWindow.webContents.executeJavaScript(`console.log("${s}")`) 
    } 
} 

main.js代碼描述:

  • let deeplinkingUrl我們保留提供的網址。我們將它設置爲deeplinkingUrl = url! (請參見// Protocol handler for osx

  • 在win32平臺上,此文件與其他參數一起保存在process.argv。要獲取僅提供的網址,deeplinkingUrl = argv.slice(1)。 (見// Protocol handler for win32

  • 由於@Aurélien Nicolas提到,在app.makeSingleInstance method我們檢查中,我們的平臺,我們設置deeplinkingUrl據此!如果我們在win32平臺上,url位於argv變量來自回調函數,否則macOS應該已經設置爲'open-url'事件! (見// Force Single Instance Application

+0

它不適用於我的窗口。如果我們在平臺上安裝應用程序,這是否可行?或者我可以以某種方式在開發模式下測試它? –

+0

@MuhammadYawarAli,如果我沒有記錯,爲了爲您的應用程序註冊一個協議處理程序,您需要使安裝程序能夠註冊它。 (使用電子生成器項目來打包我們的應用程序)所以是的,你應該先安裝應用程序。 – oikonomopo

+0

好的,我會嘗試構建一個安裝程序,但它在Mac OS甚至在開發模式下也能正常工作。 –

相關問題