diff --git a/WechatExporter/core/Exporter.cpp b/WechatExporter/core/Exporter.cpp index 382cca6..23a12e3 100755 --- a/WechatExporter/core/Exporter.cpp +++ b/WechatExporter/core/Exporter.cpp @@ -812,20 +812,10 @@ int Exporter::exportSession(const Friend& user, const MessageParser& msgParser, auto b = messages.cbegin(); // No page for text mode auto e = (((m_options & (SPO_TEXT_MODE | SPO_SYNC_LOADING)) != 0) || (messages.size() <= pageSize)) ? messages.cend() : (b + pageSize); - std::string moreMsgs = ((m_options & (SPO_TEXT_MODE | SPO_SYNC_LOADING)) != 0) ? "[]" : "[]"; // [] = empty json array - if (e != messages.cend()) - { - Json::Value jsonMsgs(Json::arrayValue); - for (auto it = e; it != messages.cend(); ++it) - { - jsonMsgs.append(*it); - } - Json::FastWriter writer; - moreMsgs = writer.write(jsonMsgs); - } - - std::string scripts = (m_options & SPO_SYNC_LOADING) || (messages.size() <= pageSize) ? "" : getTemplate("scripts"); - + + const size_t numberOfMessages = std::distance(e, messages.cend()); + const size_t numberOfPages = (numberOfMessages + pageSize - 1) / pageSize; + std::string html = getTemplate("frame"); #ifndef NDEBUG replaceAll(html, "%%USRNAME%%", user.getUsrName() + " - " + user.getHash()); @@ -835,17 +825,50 @@ int Exporter::exportSession(const Friend& user, const MessageParser& msgParser, replaceAll(html, "%%SESSION_USRNAME%%", ""); #endif replaceAll(html, "%%DISPLAYNAME%%", session.getDisplayName()); - replaceAll(html, "%%JSONDATA%%", moreMsgs); - replaceAll(html, "%%ASYNCLOADINGTYPE%%", m_loadingDataOnScroll ? "onscroll" : "initial"); + replaceAll(html, "%%ASYNC_LOADING_TYPE%%", m_loadingDataOnScroll ? "onscroll" : "initial"); + + replaceAll(html, "%%SIZE_OF_PAGE%%", std::to_string(pageSize)); + replaceAll(html, "%%NUMBER_OF_MSGS%%", std::to_string(numberOfMessages)); + replaceAll(html, "%%NUMBER_OF_PAGES%%", std::to_string(numberOfPages)); + + replaceAll(html, "%%DATA_PATH%%", encodeUrl(session.getOutputFileName() + "_files") + "/Data"); replaceAll(html, "%%BODY%%", join(b, e, "")); - - replaceAll(html, "%%LOADING_SCRIPTS%%", scripts); - replaceAll(html, "%%HEADER_FILTER%%", (m_options & SPO_SUPPORT_FILTER) ? getTemplate("filter") : ""); std::string fileName = combinePath(outputBase, session.getOutputFileName() + "." + m_extName); writeFile(fileName, html); + + if ((m_options & SPO_SYNC_LOADING) == 0 && numberOfPages > 0) + { + std::string dataPath = combinePath(outputBase, session.getOutputFileName() + "_files", "Data"); + makeDirectory(dataPath); + + for (size_t page = 0; page < numberOfPages; ++page) + { + b = e; + std::string scripts = getTemplate("scripts"); + e = (page == (numberOfPages - 1)) ? messages.cend() : (b + pageSize); + Json::Value jsonMsgs(Json::arrayValue); + for (auto it = b; it != e; ++it) + { + jsonMsgs.append(*it); + } + Json::StreamWriterBuilder builder; + builder["indentation"] = ""; // assume default for comments is None +#ifndef NDEBUG + builder["emitUTF8"] = true; +#endif + std::string moreMsgs = Json::writeString(builder, jsonMsgs); + + replaceAll(scripts, "%%JSON_DATA%%", moreMsgs); + + fileName = combinePath(dataPath, "msg-" + std::to_string(page + 1) + ".js"); + writeFile(fileName, scripts); + } + } + + } return numberOfMsgs; diff --git a/WechatExporter/res/templates/frame.html b/WechatExporter/res/templates/frame.html index 5757cfc..32fa536 100644 --- a/WechatExporter/res/templates/frame.html +++ b/WechatExporter/res/templates/frame.html @@ -449,9 +449,10 @@ } } - if (visibleMsgs < 1000 && (typeof window.moreWechatMsgs !== 'undefined') && (window.moreWechatMsgs.length > 0)) + if (visibleMsgs < window.sizeOfMsgPage && (typeof window.wechatMsgsIndexes !== 'undefined') && (window.wechatMsgsIndexes.length > 0)) { - loadMsgsForNextPage(1000 - visibleMsgs); + window.numberOfMsgsToLoad = window.sizeOfMsgPage - visibleMsgs; + loadMsgsForNextPage(); } document.body.style.cursor = 'default'; } @@ -527,9 +528,10 @@ visibleMsgs++; } } - if (visibleMsgs < 1000 && (typeof window.moreWechatMsgs !== 'undefined') && (window.moreWechatMsgs.length > 0)) + if (visibleMsgs < window.sizeOfMsgPage && (typeof window.wechatMsgsIndexes !== 'undefined') && (window.wechatMsgsIndexes.length > 0)) { - loadMsgsForNextPage(1000 - visibleMsgs); + window.numberOfMsgsToLoad = window.sizeOfMsgPage - visibleMsgs; + loadMsgsForNextPage(); } document.body.style.cursor = 'default'; @@ -548,60 +550,73 @@ { showElements("video"); } - - function loadMsgsForNextPage(numberOfMsgsToLoad) + + function loadMsgsForNextPage() { - if ((typeof window.moreWechatMsgs === 'undefined') || (window.moreWechatMsgs.length == 0)) + console.log("loadMsgsForNextPage starts..."); + + if ((typeof window.wechatMsgsIndexes === 'undefined') || (window.wechatMsgsIndexes.length == 0)) { return 0; } - - if (typeof numberOfMsgsToLoad === 'undefined') + if ((typeof window.moreWechatMsgs === 'undefined')) { - numberOfMsgsToLoad = 1000; + window.moreWechatMsgs = []; } - var fragment = document.createDocumentFragment(); - var div = document.createElement('div'); - fragment.appendChild(div); - var visibleMsgs = 0; - while (window.moreWechatMsgs.length > 0) + + var visibleMsgs = 0; + if (window.moreWechatMsgs.length > 0) { - div.innerHTML = window.moreWechatMsgs.shift(); - if (div.children.length > 0) - { - var msgDiv = div.children[0]; - fragment.appendChild(msgDiv); - - if (filterMsg(msgDiv)) - { - visibleMsgs++; - } - } + var fragment = document.createDocumentFragment(); + var div = document.createElement('div'); + fragment.appendChild(div); - if (visibleMsgs >= numberOfMsgsToLoad) + while (window.moreWechatMsgs.length > 0) { - break; + div.innerHTML = window.moreWechatMsgs.shift(); + if (div.children.length > 0) + { + var msgDiv = div.children[0]; + fragment.appendChild(msgDiv); + + if (filterMsg(msgDiv)) + { + visibleMsgs++; + } + } + + } + + fragment.removeChild(div); + var containerDiv = document.getElementById('msgs-div'); + if (null != containerDiv) + { + containerDiv.appendChild(fragment); } } - fragment.removeChild(div); - var containerDiv = document.getElementById('msgs-div'); - if (null != containerDiv) + + window.numberOfMsgsToLoad -= visibleMsgs; + if ((window.numberOfMsgsToLoad > 0) && window.wechatMsgsIndexes.length > 0) { - containerDiv.appendChild(fragment); + // Load next page + var nextPage = window.wechatMsgsIndexes.shift(); + + console.log("load next page:" + nextPage); + + var script = document.createElement("script"); + script.type = "text/javascript"; + script.src = "%%DATA_PATH%%/msg-" + nextPage + ".js"; + document.body.appendChild(script); } - + else + { + console.log("(visibleMsgs(" + visibleMsgs + ") < window.numberOfMsgsToLoad(" + + window.numberOfMsgsToLoad + ") ) && window.wechatMsgsIndexes.length(" + window.wechatMsgsIndexes.length + ") > 0"); + } + return true; } - function loadMoreMsgs() - { - window.requestAnimationFrame(function() { - if (loadMsgsForNextPage()) - { - loadMoreMsgs(); - } - }); - } - + function checkScrollDirectionIsUp(e) { if (e.wheelDelta) @@ -632,38 +647,68 @@ - - %%LOADING_SCRIPTS%% - diff --git a/WechatExporter/res/templates/scripts.html b/WechatExporter/res/templates/scripts.html index a1ea609..5e55502 100644 --- a/WechatExporter/res/templates/scripts.html +++ b/WechatExporter/res/templates/scripts.html @@ -1,5 +1,14 @@ -