diff --git a/package.json b/package.json index 7b89412..5d654da 100644 --- a/package.json +++ b/package.json @@ -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" } diff --git a/public/scripts/controllers/edit-controller.js b/public/scripts/controllers/edit-controller.js index ffc5379..4dc318d 100644 --- a/public/scripts/controllers/edit-controller.js +++ b/public/scripts/controllers/edit-controller.js @@ -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) { diff --git a/public/scripts/services/bookmark-service.js b/public/scripts/services/bookmark-service.js index 0364551..ba64c17 100644 --- a/public/scripts/services/bookmark-service.js +++ b/public/scripts/services/bookmark-service.js @@ -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/', { diff --git a/routes/api.js b/routes/api.js index 06fa9bd..93e3e0e 100644 --- a/routes/api.js +++ b/routes/api.js @@ -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(/]*?)>/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);