2011-10-05 8 views
28

संभव डुप्लिकेट एक Node.js कमांड लाइन स्क्रिप्ट से एक ब्राउज़र शुरू होगा:
How to use nodejs to open default browser and navigate to a specific URLकैसे आप

अगर यह मायने रखती है मैं नहीं जानता, लेकिन मैं पर OSX हूँ ।

मुझे पता है तुम लिखकर आदेश पंक्ति से ही एक ब्राउज़र शुरू कर सकते हैं:

open http://www.stackoverflow.com 

लेकिन वहाँ एक NodeJS कमांड लाइन स्क्रिप्ट के अंदर से एक ब्राउज़र को खोलने के लिए एक रास्ता है?

उत्तर

64

Opn अब मौजूद है, का उपयोग करें। :)

साथ स्थापित करें:

const opn = require('opn'); 

// Opens the image in the default image viewer 
opn('unicorn.png').then(() => { 
    // image viewer closed 
}); 

// Opens the url in the default browser 
opn('http://sindresorhus.com'); 

// Specify the app to open in 
opn('http://sindresorhus.com', {app: 'firefox'}); 

// Specify app arguments 
opn('http://sindresorhus.com', {app: ['google chrome', '--incognito']});var open = require('open'); 

    open('http://www.google.com'); 

You can also select a specific browser: 

    open('http://www.google.com', 'firefox'); 

Or handle an error callback: 

    open('http://www.google.com', function (err) { 
     if (err) throw err; 
     console.log('The user closed the browser'); 
    }); 
+4

क्या लिनक्स और विंडोज के बारे में: के साथ

$ npm install --save opn 

का उपयोग करें? – chovy

+2

मेरा मानना ​​है कि एक्सडीजी-ओपन लिनक्स के लिए खुले मानक संस्करण है, लेकिन मुझे नहीं पता कि आप विंडोज के लिए क्या उपयोग करेंगे। – ctide

+9

https://github.com/fixedset/open.js एक्स-प्लेटफ़ॉर्म समस्या –