Crawling the Wiki and refactor it the good way
当前为
// ==UserScript==
// @name DSA Wiki Crawler
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Crawling the Wiki and refactor it the good way
// @author David Mahl
// @match http://www.ulisses-regelwiki.de/index.php/sonderfertigkeiten.html
// @grant none
// @require http://code.jquery.com/jquery-latest.js
// @require https://cdn.jsdelivr.net/lodash/4.14.2/lodash.min.js
// ==/UserScript==
(function() {
'use strict';
var stop = false,
storage = {},
app = {
getAllNavigationEntrys: function(result, entryPoint) { // entryPoint = a.b.c //String
var $itemQuery = $('nav table a[role="menuitem"]');
if (result !== undefined) {
$itemQuery = $(result).find('nav table a[role="menuitem"]');
}
console.log('entryPoint:',entryPoint);
console.log('$itemQuery:',$itemQuery);
$itemQuery.each(function(i, e) {
var title = $(this).attr('title'),
href= $(this).attr('href'),
objectPath= entryPoint === undefined?'root.'+title:entryPoint+'.'+title;
console.log('Href: ',href);
console.log('ObjectPath:',objectPath);
_.setWith(storage, objectPath, {'title':title, 'href':href, 'children':{}});
console.log('Storage:',storage);
if (stop === false) {
console.log('ObjectPath:',objectPath + '.href');
app.crawlSubNavigation(_.get(storage, objectPath + '.href'), objectPath+'.children');
}
});
console.log('Storage', storage, ' ');
console.log('stop:',stop);
},
crawlSubNavigation: function(url, parent) {
console.log('crawlSubNavigation', parent);
stop = true;
app.loadPage(url)
.then(app.parsePage.bind(null, parent))
.fail(function (err) {
throw new Error('Error while Loading Time');
});
},
parsePage: function(entryPoint, result) {
console.log('ParsePage');
app.getAllNavigationEntrys(result, entryPoint);
},
loadPage: function(url) {
console.log('LoadPage:',url);
return $.ajax( {
url: url,
type: "POST",
dataType: "html"
});
},
};
app.parsePage();
})();