From 277eff3803a525055d4826b2cbc3b557fe3b77b0 Mon Sep 17 00:00:00 2001 From: Yu-Jen Lin Date: Mon, 15 May 2017 17:19:35 +0800 Subject: [PATCH 1/3] fixed undefined variable on titlebar --- renderer-process/components/titlebar.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/renderer-process/components/titlebar.js b/renderer-process/components/titlebar.js index a40c532..1ac0cb2 100644 --- a/renderer-process/components/titlebar.js +++ b/renderer-process/components/titlebar.js @@ -19,6 +19,7 @@ var TitleBar = function(options) { var $element = $(html); var element = $element[0]; this.element = element; + this.$element = $element; var self = this; var close = $('.titlebar-close', element)[0]; @@ -53,7 +54,8 @@ TitleBar.prototype.appendTo = function(target) { if(typeof target === 'string') target = $(target)[0]; var $element = this.$element; - + console.log($element); + console.log(this.element); $window.on('keydown', this._onkeydown = function(e) { if(e.keyCode === 18) { From 9482f16d031bcdbc8b1ffc7d1e0066626e6a9089 Mon Sep 17 00:00:00 2001 From: Yu-Jen Lin Date: Mon, 15 May 2017 19:08:28 +0800 Subject: [PATCH 2/3] clean up and set request timeout to 5000ms --- renderer-process/components/titlebar.js | 7 +- renderer-process/content-loader.js | 16 +- renderer-process/favorite-view.js | 141 ------------- renderer-process/load-after.js | 10 +- renderer-process/parsers/8comic.js | 140 +++++++------ renderer-process/parsers/dm5.js | 134 ++++++------ .../parsers/readcomicbooksonline.js | 143 ++++++------- renderer-process/parsers/sfacg.js | 101 ++++----- renderer-process/util.js | 31 ++- .../viewcontrollers/about-viewcontroller.js | 6 +- .../favorite-viewcontroller.js | 77 ++++--- .../viewcontrollers/read-viewcontroller.js | 193 ++++++++---------- .../viewcontrollers/search-viewcontroller.js | 69 +++---- .../titlebar-viewcontroller.js | 35 ++-- .../translate-viewcontroller.js | 10 +- .../view-switch-viewcontroller.js | 20 +- .../viewmodels/comicparse-viewmodel.js | 74 ++++--- .../viewmodels/search-viewmodel.js | 22 +- .../viewmodels/subscribe-viewmodel.js | 86 ++++---- 19 files changed, 544 insertions(+), 771 deletions(-) delete mode 100644 renderer-process/favorite-view.js diff --git a/renderer-process/components/titlebar.js b/renderer-process/components/titlebar.js index 1ac0cb2..5d42c04 100644 --- a/renderer-process/components/titlebar.js +++ b/renderer-process/components/titlebar.js @@ -1,13 +1,11 @@ var events = require('events'); var util = require('util'); var fs = require('fs'); -// var domify = require('domify'); -// var $ = require("dombo"); var $window = $(window); var html_mac = fs.readFileSync(__dirname + '/../../sections/titlebar-mac.html', 'utf-8'); var html_win = fs.readFileSync(__dirname + '/../../sections/titlebar-win.html', 'utf-8'); -// console.log(html); + var TitleBar = function(options) { if (!(this instanceof TitleBar)) return new TitleBar(options); events.EventEmitter.call(this); @@ -54,8 +52,6 @@ TitleBar.prototype.appendTo = function(target) { if(typeof target === 'string') target = $(target)[0]; var $element = this.$element; - console.log($element); - console.log(this.element); $window.on('keydown', this._onkeydown = function(e) { if(e.keyCode === 18) { @@ -66,7 +62,6 @@ TitleBar.prototype.appendTo = function(target) { $window.on('keyup', this._onkeyup = function(e) { if(e.keyCode === 18) $element.removeClass('alt'); }); - // console.log(typeof this.element) target.appendChild(this.element); return this; }; diff --git a/renderer-process/content-loader.js b/renderer-process/content-loader.js index 7ba926f..f404ee3 100644 --- a/renderer-process/content-loader.js +++ b/renderer-process/content-loader.js @@ -13,18 +13,4 @@ $.get('./sections/read-view.html', function(result) { $.get('./sections/about-view.html', function(result) { $("#main-view").append(result); -}) - -// $.get('./sections/page.html', function(result) { -// for(var i = 0; i < 50; i++) { -// var view = $(result); -// view.find("img").attr("src", "./assets/img/test/80x100.png") -// $("#read-area").append(view); -// } - -// }) - -// tmp.load('./sections/search-view.html'); -// $("#main-view").append(tmp.find("loader").html()); -// tmp.load('./sections/read-view.html'); -// $("#main-view").append(tmp.find("loader").html()); \ No newline at end of file +}) \ No newline at end of file diff --git a/renderer-process/favorite-view.js b/renderer-process/favorite-view.js deleted file mode 100644 index b616492..0000000 --- a/renderer-process/favorite-view.js +++ /dev/null @@ -1,141 +0,0 @@ -/** - * Favorite View - * favorite-view.js - * - * See Also: ../sections/favorite-view.html, - * ../sections/favorite-entry.html, - * ./subscriber.js - */ - -const settings = require("electron-settings"); - - -module.exports = { - updateSubscribeUI: updateSubscribeUI, - - // Binding functions - bindRegister: bindRegister, - bindSubscribe: bindSubscribe, - bindUnsubscribe: bindUnsubscribe, - bindSelectComic: bindSelectComic -} - -/** - * Action Binding - */ -var register; -var subscribe; -var unsubscribe; -var selectComic; - -function bindRegister(func) { - register = func; -} - -function bindSubscribe(func) { - subscribe = func; -} - -function bindUnsubscribe(func) { - unsubscribe = func; -} - -function bindSelectComic(func) { - selectComic = func; -} - -/** - * Update subscription indicator UI - */ -function updateSubscribeUI() { - $("#favorite-contents").html(""); - var comics = settings.get("comic"); - - for (var host in comics) { - for (var titleKey in comics[host]) { - if (comics[host][titleKey].subscribed) { - var link = comics[host][titleKey].link; - var imguri = comics[host][titleKey].thumbnail; - var title = comics[host][titleKey].title; - - var view = createFavEntry(link, titleKey, imguri, title, host); - - if (comics[host][titleKey].hasupdate) { - view.addClass("hasupdate"); - } - $("#favorite-contents").append(view); - } - } - } - -} - -/** - * Create a favorite entry HTML DOM object - * @param {String} link : link to comic - * @param {String} titleKey : title key store in settings - * @param {String} imguri : thumbnail's url - * @param {String} title : comic's name (human-readable) - * @param {String} host : host name - */ -function createFavEntry(link, titleKey, imguri, title, host) { - var view = $(favEntryViewStr); - view.find("img").each(function(n, img) { - view.find(".thumb").css({ - 'background': '#fff url(' + imguri + ') center center no-repeat', - 'background-size': 'cover' - }); - img.remove(); - }); - view.find(".comic-name").text(title); - view.find(".host").text(host); - var lastread = settings.get("comic." + host + "." + titleKey + ".lastread"); - var newest = settings.get("comic." + host + "." + titleKey + ".newestchapter"); - view.find(".last-read").text(lastread); - view.find(".newest").text(newest) - view.attr("title", title); - view.attr("link", link); - view.attr("titlekey", titleKey); - view.attr("host", host); - - view.find(".subscribe-btn").click(function(e){ - e.stopPropagation(); - console.log(host); - console.log(titleKey); - unsubscribe(host, titleKey); - }); - - view.click(function(e){ - console.log("fav click:" + title + ", from:" + host); - selectComic(host, link, title, titleKey, imguri); - - }); - - return view; -} - - - - -/** - * Initialized - */ - -function init () { - $.get('./sections/favorite-entry.html', function(result) { - favEntryViewStr = result; - }) -} - -// init when documen is ready -function lateInit() { - // updateSubscribeUIStatus(); -} - - -/** - * Main Script - */ - -init(); -$(document).ready(lateInit); diff --git a/renderer-process/load-after.js b/renderer-process/load-after.js index b91dc2a..0eac0a3 100644 --- a/renderer-process/load-after.js +++ b/renderer-process/load-after.js @@ -3,18 +3,18 @@ */ // const path = require('path'); // 3rd party library -var schedule = require('node-schedule'); +let schedule = require('node-schedule'); // viewmodel -var subscriber = require('./viewmodels/subscribe-viewmodel'); +let subscriber = require('./viewmodels/subscribe-viewmodel'); // viewcontroller -var viewswitch_viewcontroller = require('./viewcontrollers/view-switch-viewcontroller'); +let viewswitch_viewcontroller = require('./viewcontrollers/view-switch-viewcontroller'); const isDev = require('electron-is-dev'); -var scheduledTask; +let scheduledTask; $(document).ready(function() { - console.log(viewswitch_viewcontroller) + // console.log(viewswitch_viewcontroller) if (subscriber.hasSubscription()) { viewswitch_viewcontroller.tabswitch(viewswitch_viewcontroller.TAB_NAME.FAVORITE); } else { diff --git a/renderer-process/parsers/8comic.js b/renderer-process/parsers/8comic.js index 1728077..252c14e 100644 --- a/renderer-process/parsers/8comic.js +++ b/renderer-process/parsers/8comic.js @@ -35,9 +35,9 @@ * */ -var request = require("request"); +let request = require("request"); const util = require("../util"); -var async = require('async'); +let async = require('async'); module.exports = { search: search, @@ -45,9 +45,9 @@ module.exports = { loadChapter: loadChapter } -var host = "8comic"; -var searchuri = "http://8comic.se/搜尋結果/?w={search}"; -var baseuri = "http://8comic.se"; +let host = "8comic"; +let searchuri = "http://8comic.se/搜尋結果/?w={search}"; +let baseuri = "http://8comic.se"; /** * Search comic books @@ -67,10 +67,10 @@ var baseuri = "http://8comic.se"; * description {String} */ function search(search_term, callback) { - // console.log(encodeURI(searchuri.replace("{search}", search_term))) request({ method: "GET", - uri: encodeURI(searchuri.replace("{search}", search_term)) + uri: encodeURI(searchuri.replace("{search}", search_term)), + timeout: 5000 }, searchResponse.bind({callback:callback})); } @@ -79,18 +79,19 @@ function search(search_term, callback) { * @param see npm request module */ function searchResponse(error, response, body) { - // console.log(body); - var tmp = $("#post-35 .post-list-full", "
" + body + "
").find('.data a'); - var result = []; - var callback = this.callback; + let tmp = $("#post-35", "
" + body + "
").find('.post-list-full .data a'); + let result = []; + let callback = this.callback; async.each(tmp, function(e, callback1){ - var title = $(e).text(); - var link = baseuri + $(e).attr('href'); - var rel_link = $(e).attr('href'); - var titlekey = $(e).attr('href').substr(1, rel_link.length - 2); + let $e = $(e); + let title = $e.text(); + let link = baseuri + $e.attr('href'); + let rel_link = $e.attr('href'); + let titlekey = $e.attr('href').substr(1, rel_link.length - 2); request({ method: "GET", - uri: link + uri: link, + timeout: 5000 }, onDetailInfoGet.bind({ callback: callback1, link: link, @@ -108,24 +109,26 @@ function searchResponse(error, response, body) { this.callback(result, host); } +/** + * Since 8Comic's search result does not return detail information, we will + * need another GET request to get those information + * @param see npm request module + */ function onDetailInfoGet(error, response, body) { - var tmp = $("#content table tr:first-child", "
" + body + "
"); - // console.log(this.title); - // console.log(this.titleKey); + let tmp = $("#content", "
" + body + "
").find('table tr:first-child'); - var imguri = tmp.find('img').attr('src'); - var description = tmp.find('p').text().replace(/^\s+|\s+$/g, ''); - // console.log(imgurl); - // console.log(description); - - var obj = {}; - obj.link = this.link; - obj.titlekey = this.titlekey; - obj.imguri = imguri; - obj.title = this.title; - obj.host = host; - obj.updateinfo = ""; - obj.description = description; + let imguri = tmp.find('img').attr('src'); + let description = tmp.find('p').text().replace(/^\s+|\s+$/g, ''); + + let obj = { + link: this.link, + titlekey: this.titlekey, + imguri: imguri, + title: this.title, + host: host, + updateinfo: "", + description: description + } this.result.push(obj); this.callback(); @@ -153,28 +156,33 @@ function onDetailInfoGet(error, response, body) { function grabChapters(titlekey, link, callback) { request({ methos: 'GET', - uri: link + uri: link, + timeout: 5000 }, onChapterGrabbed.bind({callback: callback, titlekey: titlekey})); } +/** + * + * @param see npm request module + */ function onChapterGrabbed(error, response, body) { - var hostpath = response.request.host; - var tmp = $("#content table tr:nth-child(n+3):nth-last-child(n+1)", "
" + body + "
").find('td'); - var result = []; - var newest = ""; - var titlekey = this.titlekey; + let hostpath = response.request.host; + let tmp = $("#content", "
" + body + "
").find('table tr:nth-child(n+3):nth-last-child(n+1) td'); + let result = []; + let newest = ""; + let titlekey = this.titlekey; tmp.each(function(i, e){ - if($(e).find('a').attr('href') == undefined) return; - var ch_name = $(e).find('a').text(); - var ch_link = $(e).find('a').attr('href'); - var ch_group = "cr_main"; - var link_chunks = $(e).find('a').attr('href').split('/'); - var lastIndex = link_chunks.length; - var domid = link_chunks[lastIndex-1] == ""? link_chunks[lastIndex-2]:link_chunks[lastIndex-1]; - var ch_key = domid; - // console.log(chName + ":" + chLink + ":" + chGroup + ":" + domid); - - var obj = { + let $e = $(e); + if($e.find('a').attr('href') == undefined) return; + let ch_name = $e.find('a').text(); + let ch_link = $e.find('a').attr('href'); + let ch_group = "cr_main"; + let link_chunks = $e.find('a').attr('href').split('/'); + let lastIndex = link_chunks.length; + let domid = link_chunks[lastIndex-1] == ""? link_chunks[lastIndex-2]:link_chunks[lastIndex-1]; + let ch_key = domid; + + let obj = { ch_name: ch_name, ch_link: ch_link, ch_group: ch_group, @@ -184,10 +192,9 @@ function onChapterGrabbed(error, response, body) { }; result.unshift(obj) }); - for (var i = 0; i < result.length; i++) { + for (let i = 0; i < result.length; i++) { result[i].index = i; } - // console.log(result); newest = result[0].ch_name; this.callback(result, newest); @@ -209,7 +216,8 @@ function onChapterGrabbed(error, response, body) { function loadChapter(ch_link, ch_group, ch_key, callback) { request({ method: 'GET', - uri: ch_link + uri: ch_link, + timeout: 5000 }, onSingleChapterLoaded.bind({callback:callback, ch_group: ch_group, ch_key: ch_key})) } @@ -221,21 +229,19 @@ function loadChapter(ch_link, ch_group, ch_key, callback) { * @param see npm request module */ function onSingleChapterLoaded(error, response, body) { - // console.log(this.chKey); - var tmp = $("
" + body + "
"); - var find_script = tmp.find('#pull option:nth-child(2)').attr('value').split("'"); - var chapters_num = find_script[1]; - var num_pages = find_script[3]; - var img_template = tmp.find("#caonima").attr("src"); - console.log(img_template); - var pid = '/' + chapters_num + '/'; - var img = img_template.split(pid); - var result = []; - for (var i = 1; i <= num_pages; i++) { - var src = img[0] + pid + util.pad(i, 3) + '.jpg'; - // console.log(src); - var id = 'pic' + i; - var obj = { + let tmp = $("
" + body + "
"); + let find_script = tmp.find('#pull').find('option:nth-child(2)').attr('value').split("'"); + let chapters_num = find_script[1]; + let num_pages = find_script[3]; + let img_template = tmp.find("#caonima").attr("src"); + + let pid = '/' + chapters_num + '/'; + let img = img_template.split(pid); + let result = []; + for (let i = 1; i <= num_pages; i++) { + let src = img[0] + pid + util.pad(i, 3) + '.jpg'; + let id = 'pic' + i; + let obj = { imgurl: src, id: id, idx: i-1 diff --git a/renderer-process/parsers/dm5.js b/renderer-process/parsers/dm5.js index 007737d..89547cc 100644 --- a/renderer-process/parsers/dm5.js +++ b/renderer-process/parsers/dm5.js @@ -35,18 +35,18 @@ * */ -var request = require("request"); +let request = require("request"); const util = require("../util"); -var async = require('async'); +let async = require('async'); module.exports = { search: search, grabChapters: grabChapters, loadChapter: loadChapter } -var host = "dm5"; -var searchuri = "http://www.dm5.com/search?title={search}&language=1"; -var baseuri = "http://www.dm5.com/"; +let host = "dm5"; +let searchuri = "http://www.dm5.com/search?title={search}&language=1"; +let baseuri = "http://www.dm5.com/"; /** * Search comic books @@ -66,7 +66,6 @@ var baseuri = "http://www.dm5.com/"; * description {String} */ function search(search_term, callback) { - // console.log(encodeURI(searchuri.replace("{search}", search_term))) request({ method: "GET", uri: encodeURI(searchuri.replace("{search}", search_term)), @@ -75,7 +74,8 @@ function search(search_term, callback) { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36', 'Accept-Language': 'en-US,en;q=0.8,zh-TW;q=0.6,zh;q=0.4,ja;q=0.2', cookie: 'isAdult=1' - } + }, + timeout: 5000 }, searchResponse.bind({callback:callback})); } @@ -84,31 +84,27 @@ function search(search_term, callback) { * @param see npm request module */ function searchResponse(error, response, body) { - // console.log(body); - // var tmp2 = $("
" + body + "
"); - // console.log(tmp2.html()); - var tmp = $(".container .midBar .item", "
" + body + "
"); - // console.log (tmp); - var result = []; - var callback = this.callback; + let tmp = $(".container .midBar .item", "
" + body + "
"); + let result = []; + let callback = this.callback; async.each(tmp, function(e, callback1){ - var title = $(e).find('a.title').text(); - // console.log(title); - var rel_link = $(e).find('a.title').attr('href'); - var titlekey = rel_link.substring(1, rel_link.length - 2); - var link = baseuri + rel_link; - var imguri = $(e).find('img').attr('src'); - var updateinfo = "作者:" + $(e).find('dt>a:first-of-type').text(); - // console.log(updateinfo); - var description = $(e).find('.info .value').text().replace(/^\s+|\s+$/g, ''); - var obj = {}; - obj.link = link; - obj.titlekey = titlekey; - obj.imguri = imguri; - obj.title = title; - obj.host = host; - obj.updateinfo = updateinfo; - obj.description = description; + let $e = $(e); + let title = $e.find('a.title').text(); + let rel_link = $e.find('a.title').attr('href'); + let titlekey = rel_link.substring(1, rel_link.length - 2); + let link = baseuri + rel_link; + let imguri = $e.find('img').attr('src'); + let updateinfo = "作者:" + $e.find('dt>a:first-of-type').text(); + let description = $(e).find('.info .value').text().replace(/^\s+|\s+$/g, ''); + let obj = { + link: link, + titlekey: titlekey, + imguri: imguri, + title: title, + host: host, + updateinfo: updateinfo, + description: description + } result.push(obj); callback1(); }, function () { @@ -116,7 +112,6 @@ function searchResponse(error, response, body) { callback(result, host); }); - // this.callback(result, host); } @@ -147,32 +142,33 @@ function grabChapters(titlekey, link, callback) { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36', 'Accept-Language': 'en-US,en;q=0.8,zh-TW;q=0.6,zh;q=0.4,ja;q=0.2', cookie: 'isAdult=1' - } + }, + timeout: 5000 }, onChapterGrabbed.bind({callback: callback, titlekey: titlekey})); } +/** + * + * @param see npm request module + */ function onChapterGrabbed(error, response, body) { - // var hostpath = response.request.host; - var tmp = $("
" + body + "
").find('[id^=cbc] li a'); - var result = []; - var result_keys = {}; - var newest = ""; - var titlekey = this.titlekey; + // let hostpath = response.request.host; + let tmp = $("
" + body + "
").find('[id^=cbc] li a'); + let result = []; + let result_keys = {}; + let newest = ""; + let titlekey = this.titlekey; tmp.each(function(i, e){ if($(e).attr('href') == undefined) return; - var ch_name = $(e).text().replace(/.*(?:漫畫|漫画)\s*/g, ''); - // console.log(ch_name); - var rel_link = $(e).attr('href'); + let ch_name = $(e).text().replace(/.*(?:漫畫|漫画)\s*/g, ''); + let rel_link = $(e).attr('href'); if (rel_link.includes("javascript")) return; - var ch_link = baseuri + rel_link; - var ch_group = "cr_main"; - // var link_chunks = $(e).find('a').attr('href').split('/'); - // var lastIndex = link_chunks.length; + let ch_link = baseuri + rel_link; + let ch_group = "cr_main"; - var domid = rel_link.replace(/\//g, ''); - var ch_key = domid; - // // console.log(chName + ":" + chLink + ":" + chGroup + ":" + domid); - var obj = { + let domid = rel_link.replace(/\//g, ''); + let ch_key = domid; + let obj = { ch_name: ch_name, ch_link: ch_link, ch_group: ch_group, @@ -180,7 +176,6 @@ function onChapterGrabbed(error, response, body) { domid: domid, index: i }; - // console.log(ch_name + ":" + i + ":" + ch_key); if (ch_key in result_keys) { } else { @@ -188,11 +183,6 @@ function onChapterGrabbed(error, response, body) { result_keys[ch_key] = true; } }); - // for(var i = 0; i < result.length; i++) { - // result[i].index = i; - // } - // console.log(this.titlekey) - // console.log(result); newest = result[0].ch_name; this.callback(result, newest); @@ -220,7 +210,8 @@ function loadChapter(ch_link, ch_group, ch_key, callback) { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36', 'Accept-Language': 'en-US,en;q=0.8,zh-TW;q=0.6,zh;q=0.4,ja;q=0.2', cookie: 'isAdult=1' - } + }, + timeout: 5000 }, onSingleChapterLoaded.bind({callback:callback, ch_group: ch_group, ch_key: ch_key})) } @@ -232,21 +223,18 @@ function loadChapter(ch_link, ch_group, ch_key, callback) { * @param see npm request module */ function onSingleChapterLoaded(error, response, body) { - var doc = body; - // console.log(body); - var tmp = $("
" + body + "
"); - // console.log(body); - var script1=/