后台只用cheerio作为后台的jQuery使用
This commit is contained in:
parent
ef68faa337
commit
cdb005306b
|
|
@ -1,59 +1,52 @@
|
||||||
var jsdom = require("jsdom");
|
var cheerio = require("cheerio");
|
||||||
var fs = require("fs");
|
var fs = require("fs");
|
||||||
|
|
||||||
var parsehtml = function(file, callback) {
|
var parsehtml = function(file, callback) {
|
||||||
// var html = fs.readFileSync(file).toString();
|
// var html = fs.readFileSync(file).toString();
|
||||||
|
|
||||||
fs.readFile(file, (err, data) => {
|
fs.readFile(file, (err, data) => {
|
||||||
if (err) throw err;
|
if (err) throw err;
|
||||||
var html = data.toString();
|
var html = data.toString();
|
||||||
jsdom.env({
|
var $, anchors, itemdoubleclick, results, allTags, bookmarks;
|
||||||
html: html,
|
$ = cheerio.load(html);
|
||||||
scripts: ["./public/scripts/externe/jquery-3.1.1.min.js"],
|
itemdoubleclick = "";
|
||||||
done: function(errors, window) {
|
allTags = [];
|
||||||
var $, anchors, itemdoubleclick, results, allTags, bookmarks;
|
bookmarks = [];
|
||||||
$ = window.$;
|
results = {};
|
||||||
itemdoubleclick = "";
|
|
||||||
allTags = [];
|
|
||||||
bookmarks = [];
|
|
||||||
results = {};
|
|
||||||
|
|
||||||
anchors = $("dl").find("a");
|
anchors = $("dl").find("a");
|
||||||
anchors.each(function(i, e) {
|
anchors.each(function(i, e) {
|
||||||
var add_date, name, bookmark, tags, url;
|
var add_date, name, bookmark, tags, url;
|
||||||
url = $(e).attr("href");
|
url = $(e).attr("href");
|
||||||
name = $(e).text();
|
name = $(e).text();
|
||||||
add_date = $(e).attr("add_date");
|
add_date = $(e).attr("add_date");
|
||||||
tags = new Array();
|
tags = new Array();
|
||||||
$(e).parents("dl").each(function(ii, ee) {
|
$(e).parents("dl").each(function(ii, ee) {
|
||||||
var folder, tag;
|
var folder, tag;
|
||||||
folder = $(ee).prev();
|
folder = $(ee).prev();
|
||||||
tag = folder.text().replace(/(^\s*)|(\s*$)/g, '').replace(/\s+/g, ' ');
|
tag = folder.text().replace(/(^\s*)|(\s*$)/g, '').replace(/\s+/g, ' ');
|
||||||
if (tag != "Bookmarks" && tag != "书签栏" && tag != "") {
|
if (tag != "Bookmarks" && tag != "书签栏" && tag != "") {
|
||||||
if (allTags.indexOf(tag) == -1) {
|
if (allTags.indexOf(tag) == -1) {
|
||||||
allTags.push(tag);
|
allTags.push(tag);
|
||||||
}
|
}
|
||||||
return tags.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.");
|
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
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;
|
module.exports = parsehtml;
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,6 @@
|
||||||
"download": "^5.0.3",
|
"download": "^5.0.3",
|
||||||
"express": "~4.13.4",
|
"express": "~4.13.4",
|
||||||
"express-session": "^1.14.1",
|
"express-session": "^1.14.1",
|
||||||
"jsdom": "^9.10.0",
|
|
||||||
"morgan": "^1.8.1",
|
"morgan": "^1.8.1",
|
||||||
"multer": "^1.3.0",
|
"multer": "^1.3.0",
|
||||||
"mysql": "^2.11.1",
|
"mysql": "^2.11.1",
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue