在添加书签页面,输入书签页面,能自动填写书签标题

This commit is contained in:
luchenqun 2016-11-05 21:46:25 +08:00
parent dbc7ba3816
commit 82d6fbb99a
4 changed files with 90 additions and 1 deletions

View File

@ -7,14 +7,17 @@
},
"dependencies": {
"body-parser": "~1.15.1",
"cheerio": "^0.22.0",
"cookie-parser": "~1.4.3",
"crypto": "0.0.3",
"debug": "~2.2.0",
"ejs": "~2.4.1",
"express": "~4.13.4",
"express-session": "^1.14.1",
"iconv-lite": "^0.4.13",
"morgan": "~1.7.0",
"mysql": "^2.11.1",
"request": "^2.78.0",
"serve-favicon": "~2.3.0",
"supervisor": "^0.11.0"
}

View File

@ -4,10 +4,23 @@ app.controller('editCtr', ['$scope', '$state', '$timeout', 'bookmarkService', 'p
init();
semanticInit();
$scope.$watch('url', function(newValue, oldValue, scope) {
$scope.$watch('url', function(newUrl, oldUrl, scope) {
$timeout(function() {
$scope.urlError = $scope.url == '' && $('.ui.modal.js-add-bookmark').modal('is active');
});
$scope.title = "";
if (/http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/.test(newUrl)) {
var params = {
url: newUrl,
}
bookmarkService.getTitle(params).then(
function(data) {
console.log(JSON.stringify(data));
$scope.title = data.title;
},
function(errorMsg) {}
);
}
});
$scope.$watch('title', function(newValue, oldValue, scope) {

View File

@ -1,6 +1,20 @@
app.factory('bookmarkService', ['$http', '$q', function($http, $q) {
// service interface
var service = {
getTitle: function(params) {
var def = $q.defer();
$http.post('/api/getTitle/', {
params: params
})
.success(function(data) {
def.resolve(data);
})
.error(function(data) {
console.log('Error: ' + data);
def.reject('Failed to get getTitle');
});
return def.promise;
},
login: function(params) {
var def = $q.defer();
$http.post('/api/login/', {

View File

@ -1,6 +1,11 @@
var api = require('express').Router();
var mysql = require('mysql');
var crypto = require('crypto');
var http = require('http');
var https = require('https');
var cheerio = require('cheerio');
var request = require('request')
var iconv = require('iconv-lite')
var client = mysql.createConnection({
host: '127.0.0.1',
user: 'lcq',
@ -11,6 +16,60 @@ var client = mysql.createConnection({
});
client.connect();
api.post('/getTitle', function(req, response) {
var params = req.body.params;
var url = params.url;
var options = {
url: url,
encoding: null,
//代理服务器
//proxy: 'http://xxx.xxx.xxx.xxx:8888',
headers: {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36'
}
}
request(options, function(err, res, body) {
var charset = "utf-8";
var arr = body.toString().match(/<meta([^>]*?)>/g);
if (arr) {
arr.forEach(function(val) {
var match = val.match(/charset\s*=\s*(.+)\"/);
if (match && match[1]) {
if (match[1].substr(0, 1) == '"') match[1] = match[1].substr(1);
charset = match[1].trim();
return false;
}
})
}
var html = iconv.decode(body, charset);
var $ = cheerio.load(html, {
decodeEntities: false
})
var title = $("title").text();
console.log(title);
response.json({
title: title || '',
});
})
// var httpGet = url.indexOf("https") >= 0 ? https : http;
// httpGet.get(url, function(res) {
// var html = '';
// res.on('data', function(data) {
// html += data;
// });
// res.on('end', function() {
// var $ = cheerio.load(html);
// var title = $("title").text();
// console.log(title, response.headers['content-type']);
// response.json({
// title: title || '',
// });
// });
// });
})
api.post('/logout', function(req, res) {
var params = req.body.params;
console.log('logout......', params);