2
2
mirror of https://github.com/Llewellynvdm/nativefier.git synced 2024-12-23 02:28:55 +00:00

Implement scraping of the web page name for the app

This commit is contained in:
Jia Hao 2016-01-19 00:40:24 +08:00
parent a57d00f701
commit 3b02889435
3 changed files with 60 additions and 17 deletions

View File

@ -24,9 +24,11 @@
"homepage": "https://github.com/jiahaog/nativefier#readme", "homepage": "https://github.com/jiahaog/nativefier#readme",
"dependencies": { "dependencies": {
"async": "^1.5.2", "async": "^1.5.2",
"cheerio": "^0.19.0",
"commander": "^2.9.0", "commander": "^2.9.0",
"electron-packager": "^5.2.1", "electron-packager": "^5.2.1",
"ncp": "^2.0.0", "ncp": "^2.0.0",
"request": "^2.67.0",
"tmp": "0.0.28", "tmp": "0.0.28",
"validator": "^4.5.0" "validator": "^4.5.0"
}, },

View File

@ -2,6 +2,7 @@
import path from 'path'; import path from 'path';
import program from 'commander'; import program from 'commander';
import async from 'async';
import optionsFactory from './options'; import optionsFactory from './options';
import buildApp from './buildApp'; import buildApp from './buildApp';
@ -9,21 +10,28 @@ import buildApp from './buildApp';
const packageJson = require(path.join('..', 'package')); const packageJson = require(path.join('..', 'package'));
function main(program) { function main(program) {
const options = optionsFactory(
program.appName,
program.targetUrl,
program.platform,
program.arch,
program.electronVersion,
program.outDir,
program.overwrite,
program.conceal,
program.icon,
program.badge,
program.width,
program.height);
buildApp(options, (error, appPath) => { async.waterfall([
callback => {
optionsFactory(
program.appName,
program.targetUrl,
program.platform,
program.arch,
program.electronVersion,
program.outDir,
program.overwrite,
program.conceal,
program.icon,
program.badge,
program.width,
program.height, callback);
},
(options, callback) => {
buildApp(options, callback);
}
], (error, appPath) => {
if (error) { if (error) {
console.error(error); console.error(error);
return; return;

View File

@ -1,12 +1,15 @@
import os from 'os'; import os from 'os';
import path from 'path'; import path from 'path';
import request from 'request';
import cheerio from 'cheerio';
import validator from 'validator'; import validator from 'validator';
const TEMPLATE_APP_DIR = path.join(__dirname, '../', 'app'); const TEMPLATE_APP_DIR = path.join(__dirname, '../', 'app');
const ELECTRON_VERSION = '0.36.4'; const ELECTRON_VERSION = '0.36.4';
const DEFAULT_APP_NAME = 'My App';
function optionsFactory(name = 'MyApp', function optionsFactory(name,
targetUrl = 'http://google.com', targetUrl = 'http://google.com',
platform = detectPlatform(), platform = detectPlatform(),
arch = detectArch(), arch = detectArch(),
@ -17,13 +20,13 @@ function optionsFactory(name = 'MyApp',
icon, icon,
badge = false, badge = false,
width = 1280, width = 1280,
height = 800) { height = 800, callback) {
if (!validator.isURL(targetUrl, {require_protocol: true})) { if (!validator.isURL(targetUrl, {require_protocol: true})) {
throw 'Your Url is invalid!, did you remember to include \'http://\'?'; throw 'Your Url is invalid!, did you remember to include \'http://\'?';
} }
return { const options = {
dir: TEMPLATE_APP_DIR, dir: TEMPLATE_APP_DIR,
name: name, name: name,
@ -45,6 +48,23 @@ function optionsFactory(name = 'MyApp',
width: width, width: width,
height: height height: height
} }
if (name && name.length > 0) {
options.name = name;
callback(null, options);
return;
}
getTitle(options.targetUrl, function (error, pageTitle) {
if (error) {
console.warn(`Unable to automatically determine app name, falling back to '${DEFAULT_APP_NAME}'`);
options.name = DEFAULT_APP_NAME;
} else {
options.name = pageTitle;
}
callback(null, options);
});
} }
function detectPlatform() { function detectPlatform() {
@ -65,4 +85,17 @@ function detectArch() {
return os.arch(); return os.arch();
} }
function getTitle(url, callback) {
request(url, (error, response, body) => {
if (error || response.statusCode !== 200) {
callback(`Request Error: ${error}, Status Code ${response.statusCode}`);
return;
}
const $ = cheerio.load(body);
const pageTitle = $("title").text();
callback(null, pageTitle);
});
}
export default optionsFactory; export default optionsFactory;