From cdb005306bfa00809a9a3d651aad39b5ee96e757 Mon Sep 17 00:00:00 2001 From: luchenqun Date: Sat, 24 Jun 2017 21:14:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8E=E5=8F=B0=E5=8F=AA=E7=94=A8cheerio?= =?UTF-8?q?=E4=BD=9C=E4=B8=BA=E5=90=8E=E5=8F=B0=E7=9A=84jQuery=E4=BD=BF?= =?UTF-8?q?=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/parse_html.js | 85 ++++++++++++++++++++------------------------ package.json | 1 - 2 files changed, 39 insertions(+), 47 deletions(-) diff --git a/common/parse_html.js b/common/parse_html.js index 2698aa7..51644bd 100644 --- a/common/parse_html.js +++ b/common/parse_html.js @@ -1,59 +1,52 @@ -var jsdom = require("jsdom"); +var cheerio = require("cheerio"); var fs = require("fs"); var parsehtml = function(file, callback) { // var html = fs.readFileSync(file).toString(); - fs.readFile(file, (err, data) => { if (err) throw err; var html = data.toString(); - jsdom.env({ - html: html, - scripts: ["./public/scripts/externe/jquery-3.1.1.min.js"], - done: function(errors, window) { - var $, anchors, itemdoubleclick, results, allTags, bookmarks; - $ = window.$; - itemdoubleclick = ""; - allTags = []; - bookmarks = []; - results = {}; + var $, anchors, itemdoubleclick, results, allTags, bookmarks; + $ = cheerio.load(html); + itemdoubleclick = ""; + allTags = []; + bookmarks = []; + results = {}; - anchors = $("dl").find("a"); - anchors.each(function(i, e) { - var add_date, name, bookmark, tags, url; - url = $(e).attr("href"); - name = $(e).text(); - add_date = $(e).attr("add_date"); - tags = new Array(); - $(e).parents("dl").each(function(ii, ee) { - var folder, tag; - folder = $(ee).prev(); - tag = folder.text().replace(/(^\s*)|(\s*$)/g, '').replace(/\s+/g, ' '); - if (tag != "Bookmarks" && tag != "书签栏" && tag != "") { - if (allTags.indexOf(tag) == -1) { - allTags.push(tag); - } - return tags.push(tag); - } - }); - bookmark = { - url: url, - name: name, - add_date: add_date, - tags: tags - }; - return bookmarks.push(bookmark); - }); - if (typeof callback === "function") { - results.tags = allTags; - results.bookmarks = bookmarks; - return callback(results); - } else { - return console.warn("Callback isn't a function."); + anchors = $("dl").find("a"); + anchors.each(function(i, e) { + var add_date, name, bookmark, tags, url; + url = $(e).attr("href"); + name = $(e).text(); + add_date = $(e).attr("add_date"); + tags = new Array(); + $(e).parents("dl").each(function(ii, ee) { + var folder, tag; + folder = $(ee).prev(); + tag = folder.text().replace(/(^\s*)|(\s*$)/g, '').replace(/\s+/g, ' '); + if (tag != "Bookmarks" && tag != "书签栏" && tag != "") { + if (allTags.indexOf(tag) == -1) { + allTags.push(tag); + } + return tags.push(tag); } - } + }); + bookmark = { + url: url, + name: name, + add_date: add_date, + tags: tags + }; + return bookmarks.push(bookmark); }); - }); + if (typeof callback === "function") { + results.tags = allTags; + results.bookmarks = bookmarks; + return callback(results); + } else { + return console.warn("Callback isn't a function."); + } + }) }; module.exports = parsehtml; diff --git a/package.json b/package.json index a580345..c13e2d4 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,6 @@ "download": "^5.0.3", "express": "~4.13.4", "express-session": "^1.14.1", - "jsdom": "^9.10.0", "morgan": "^1.8.1", "multer": "^1.3.0", "mysql": "^2.11.1",