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",
"dependencies": {
"async": "^1.5.2",
"cheerio": "^0.19.0",
"commander": "^2.9.0",
"electron-packager": "^5.2.1",
"ncp": "^2.0.0",
"request": "^2.67.0",
"tmp": "0.0.28",
"validator": "^4.5.0"
},

View File

@ -2,6 +2,7 @@
import path from 'path';
import program from 'commander';
import async from 'async';
import optionsFactory from './options';
import buildApp from './buildApp';
@ -9,21 +10,28 @@ import buildApp from './buildApp';
const packageJson = require(path.join('..', 'package'));
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) {
console.error(error);
return;

View File

@ -1,12 +1,15 @@
import os from 'os';
import path from 'path';
import request from 'request';
import cheerio from 'cheerio';
import validator from 'validator';
const TEMPLATE_APP_DIR = path.join(__dirname, '../', 'app');
const ELECTRON_VERSION = '0.36.4';
const DEFAULT_APP_NAME = 'My App';
function optionsFactory(name = 'MyApp',
function optionsFactory(name,
targetUrl = 'http://google.com',
platform = detectPlatform(),
arch = detectArch(),
@ -17,13 +20,13 @@ function optionsFactory(name = 'MyApp',
icon,
badge = false,
width = 1280,
height = 800) {
height = 800, callback) {
if (!validator.isURL(targetUrl, {require_protocol: true})) {
throw 'Your Url is invalid!, did you remember to include \'http://\'?';
}
return {
const options = {
dir: TEMPLATE_APP_DIR,
name: name,
@ -45,6 +48,23 @@ function optionsFactory(name = 'MyApp',
width: width,
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() {
@ -65,4 +85,17 @@ function detectArch() {
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;