From fff829ae44818d537717f8308f4061a7cdf9f758 Mon Sep 17 00:00:00 2001 From: ksky521 Date: Thu, 15 Jun 2017 15:18:49 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E5=A2=9E=E5=8A=A0minimatch=E5=81=9A?= =?UTF-8?q?=E6=9F=A5=E6=89=BEwebsite?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 + src/contentscripts.js | 7 +- src/read/read.jsx | 2 +- src/service/site.js | 290 ++++++++++++++--------------- src/service/storage.js | 404 +++++++++++++++++++++-------------------- webpack.config.js | 3 +- 6 files changed, 361 insertions(+), 346 deletions(-) diff --git a/package.json b/package.json index e4a6b151c..f25972cf8 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "css-loader": "^0.26.1", "expose-loader": "^0.7.1", "file-loader": "^0.9.0", + "minimatch": "^3.0.4", "postcss-cssnext": "^2.10.0", "postcss-loader": "^1.3.3", "react": "^0.14.8", diff --git a/src/contentscripts.js b/src/contentscripts.js index be4672223..a8c12d207 100644 --- a/src/contentscripts.js +++ b/src/contentscripts.js @@ -79,7 +79,6 @@ function readMode() { if ( !entry( read, focus, "聚焦", "阅读" )) return; getCurrent( mode.read ); - switch ( st.Verify( storage.current.site.name ) ) { case 0: storage.Statistics( mode.read ); @@ -142,6 +141,8 @@ function entry( current, other, ...str ) { * @param {boolean} when true, push message */ function getCurrent( mode = undefined, upicon = true ) { - if ( mode && storage.VerifyCur( mode ) ) storage.Getcur( mode ); + if ( mode && storage.VerifyCur( mode ) ) { + storage.Getcur( mode ); + } if ( upicon ) browser.runtime.sendMessage( msg.Add( msg.MESSAGE_ACTION.browser_action, { code: storage.rdstcode, url: window.location.href } )); -} \ No newline at end of file +} diff --git a/src/read/read.jsx b/src/read/read.jsx index 28d10415e..ec6ab7593 100644 --- a/src/read/read.jsx +++ b/src/read/read.jsx @@ -359,4 +359,4 @@ async function commbeautify( $target ) { $target.find( "a" ).removeAttr( "style" ); } -export { Render, Exist }; \ No newline at end of file +export { Render, Exist }; diff --git a/src/service/site.js b/src/service/site.js index d61d47c28..c23162ed2 100644 --- a/src/service/site.js +++ b/src/service/site.js @@ -1,4 +1,5 @@ -console.log( "=== simpread site load ===" ) +console.log("=== simpread site load ===") +import minimatch from 'minimatch'; /** * Get URI @@ -9,11 +10,11 @@ function getURI() { //const arr = window.location.pathname.match( /(\S+\/\b|^\/)/g ); //return `${ window.location.protocol }//${ window.location.hostname }${ arr[0] }`; const name = (pathname) => { - pathname = pathname != "/" && pathname.endsWith("/") ? pathname = pathname.replace( /\/$/, "" ) : pathname; - return pathname.replace( /\/[%@#.~a-zA-Z0-9_-]+$|^\/$/g, "" ); + pathname = pathname != "/" && pathname.endsWith("/") ? pathname = pathname.replace(/\/$/, "") : pathname; + return pathname.replace(/\/[%@#.~a-zA-Z0-9_-]+$|^\/$/g, ""); }, - path = name( window.location.pathname ); - return `${ window.location.protocol }//${ window.location.hostname }${ path }/`; + path = name(window.location.pathname); + return `${window.location.protocol}//${window.location.hostname}${path}/`; } /** @@ -23,41 +24,44 @@ function getURI() { * @param {string} url * @return {array} 0: current site; 1: current url */ -function findSitebyURL( sites, url ) { - const domain = (names)=>{ - const arr = names.replace( "www.", "" ).match( /\.\S+\.\S+/g ); - if ( arr ) { - return arr[0].substr(1); - } else { - return names.replace( "www.", "" ); - } - }, - urls = [ ...sites.keys() ], - arr = url.match( /[.a-zA-z0-9-_]+/g ), - uri = arr[1].replace( "www.", "" ), - hostname = domain( window.location.hostname ), - isroot = ()=>window.location.pathname == "/" || /\/(default|index|portal).[0-9a-zA-Z]+$/.test(window.location.pathname); - let found; - for ( const cur of urls ) { - const name = sites.get(cur).name, - sufname= domain( name ); - if ( name == "baijia.baidu.com" && url.includes(name) ) { +function findSitebyURL(sites, url) { + const domain = (names) => { + const arr = names.replace("www.", "").match(/\.\S+\.\S+/g); + if (arr) { + return arr[0].substr(1); + } else { + return names.replace("www.", ""); + } + }, + urls = [...sites.keys()], + arr = url.match(/[.a-zA-z0-9-_]+/g), + uri = arr[1].replace("www.", ""), + hostname = domain(window.location.hostname), + isroot = () => window.location.pathname == "/" || /\/(default|index|portal).[0-9a-zA-Z]+$/.test(window.location.pathname); + let found; + for (const cur of urls) { + const name = sites.get(cur).name, + sufname = domain(name); + if (name == "baijia.baidu.com" && url.includes(name)) { found = cur; break; - } else if ( !isroot() && !cur.endsWith( "*" ) && cur.replace( /^http[s]?:/, "" ) == url.replace( /^http[s]?:/, "" ) ) { + } else if (!isroot() && !cur.endsWith("*") && cur.replace(/^http[s]?:/, "") == url.replace(/^http[s]?:/, "")) { found = cur; break; } - else if ( !isroot() && cur.endsWith( "*" ) && uri.includes( sufname ) && hostname == sufname && url.includes( name ) ) { + else if (!isroot() && cur.endsWith("*") && uri.includes(sufname) && hostname == sufname && url.includes(name)) { //if ( /\/[a-zA-Z0-9]+\/\*/g.test( cur )) { // if ( suffix != url ) return undefined; //} else if ( suffix == url ) return undefined; found = cur; break; + }else if(minimatch(window.location.href, cur)){ + found = cur + break } } - if ( found ) { - return [ clone( sites.get( found )), found ]; + if (found) { + return [clone(sites.get(found)), found]; } return undefined; } @@ -68,19 +72,19 @@ function findSitebyURL( sites, url ) { * @param {string} storage.current.site.name * @param {jquery} jquery object */ -async function removeSpareTag( name, $target ) { - let [ remove, tag ] = [ false, "" ]; - if ([ "lib.csdn.net", "huxiu.com", "my.oschina.net", "caixin.com", "163.com", "steachs.com", "hacpai.com", "apprcn.com", "mp.weixin.qq.com" ].includes( name )) { - [ remove, tag ] = [ true, "p" ]; - } else if ([ "nationalgeographic.com.cn", "dgtle.com", "news.mtime.com" ].includes( name )) { - [ remove, tag ] = [ true, "div" ]; - } else if ( ["chiphell.com"].includes( name ) ) { - [ remove, tag ] = [ true, "font" ]; +async function removeSpareTag(name, $target) { + let [remove, tag] = [false, ""]; + if (["lib.csdn.net", "huxiu.com", "my.oschina.net", "caixin.com", "163.com", "steachs.com", "hacpai.com", "apprcn.com", "mp.weixin.qq.com"].includes(name)) { + [remove, tag] = [true, "p"]; + } else if (["nationalgeographic.com.cn", "dgtle.com", "news.mtime.com"].includes(name)) { + [remove, tag] = [true, "div"]; + } else if (["chiphell.com"].includes(name)) { + [remove, tag] = [true, "font"]; } - if ( remove ) { - $target.find( tag ).map( ( index, item ) => { + if (remove) { + $target.find(tag).map((index, item) => { const str = $(item).text().toLowerCase().trim(); - if ( $(item).find( "img" ).length == 0 && str == "" ) $(item).remove(); + if ($(item).find("img").length == 0 && str == "") $(item).remove(); }); } } @@ -91,19 +95,19 @@ async function removeSpareTag( name, $target ) { * @param {string} storage.current.site.name * @return {number} 0: success; -1: not exsit; -2: tieba.com; -3: chiphell.com */ -function verify( name ) { - const [ hostname, pathname, href ] = [ window.location.hostname, window.location.pathname, window.location.href ]; - if ( hostname == "tieba.baidu.com" && !href.includes( "see_lz=1" ) ) { +function verify(name) { + const [hostname, pathname, href] = [window.location.hostname, window.location.pathname, window.location.href]; + if (hostname == "tieba.baidu.com" && !href.includes("see_lz=1")) { return -2; - } else if ( hostname == "www.chiphell.com" ) { - if ( pathname == "/forum.php" && window.location.search.includes( "mod=viewthread" ) ) { + } else if (hostname == "www.chiphell.com") { + if (pathname == "/forum.php" && window.location.search.includes("mod=viewthread")) { return 0; - } else if ( pathname.includes( "thread" ) && window.location.search == "" ) { + } else if (pathname.includes("thread") && window.location.search == "") { return -3; } else { return -1; } - } else if ( name === "" ) { + } else if (name === "") { return -1; } else { return 0; @@ -115,194 +119,194 @@ function verify( name ) { * * @param {string} storage.current.site.name */ -async function specbeautify( name, $target ) { - switch ( name ) { +async function specbeautify(name, $target) { + switch (name) { case "sspai.com": //TO-DO - $target.find( ".relation-apps" ).remove(); - $target.find( ".ss-app-card" ).remove(); + $target.find(".relation-apps").remove(); + $target.find(".ss-app-card").remove(); break; case "post.smzdm.com": - $target.find( "img.face" ).addClass( "sr-rd-content-nobeautify" ); - $target.find( ".insert-outer img" ).addClass( "sr-rd-content-nobeautify" ); + $target.find("img.face").addClass("sr-rd-content-nobeautify"); + $target.find(".insert-outer img").addClass("sr-rd-content-nobeautify"); break; case "infoq.com": - $target.find( "img" ).map( (index, item) => { - if ( $(item).css("float") == "left" ) { - $(item).addClass( "sr-rd-content-nobeautify" ); + $target.find("img").map((index, item) => { + if ($(item).css("float") == "left") { + $(item).addClass("sr-rd-content-nobeautify"); } }); break; case "appinn.com": - $target.find( ".emoji" ).addClass( "sr-rd-content-nobeautify" ); + $target.find(".emoji").addClass("sr-rd-content-nobeautify"); break; case "hacpai.com": - $target.find( ".emoji" ).addClass( "sr-rd-content-nobeautify" ); + $target.find(".emoji").addClass("sr-rd-content-nobeautify"); break; case "douban.com": - $target.find( ".review-content" ).children().unwrap(); - $target.find( "table" ).addClass( "sr-rd-content-center" ); - $target.find( "p" ).css({"white-space": "pre-wrap"}); - $target.find( ".cc" ).removeClass(); + $target.find(".review-content").children().unwrap(); + $target.find("table").addClass("sr-rd-content-center"); + $target.find("p").css({ "white-space": "pre-wrap" }); + $target.find(".cc").removeClass(); break; case "qdaily.com": - $target.find( "img" ).map( (index, item) => { + $target.find("img").map((index, item) => { const $target = $(item), - height = Number.parseInt($target.css("height")); - if ( height == 0 ) $target.remove(); + height = Number.parseInt($target.css("height")); + if (height == 0) $target.remove(); }); - $target.find( ".com-insert-images" ).map( (index, item) => { + $target.find(".com-insert-images").map((index, item) => { const $target = $(item), - imgs = $target.find( "img" ).map( (index, item)=>`
${item.outerHTML}
` ), - str = imgs.get().join( "" ); - $target.empty().removeAttr( "class" ).append( str ); + imgs = $target.find("img").map((index, item) => `
${item.outerHTML}
`), + str = imgs.get().join(""); + $target.empty().removeAttr("class").append(str); }); - $target.find( ".com-insert-embed" ).remove(); + $target.find(".com-insert-embed").remove(); break; case "news.mtime.com": - $target.find( ".newspictool" ).map( ( index, item ) => { + $target.find(".newspictool").map((index, item) => { const $target = $(item), - $img = $target.find( "img" ), - $label = $target.find( "p:last" ); - $target.removeAttr( "class" ).addClass( "sr-rd-content-center" ).empty().append( $img ).append( $label ); + $img = $target.find("img"), + $label = $target.find("p:last"); + $target.removeAttr("class").addClass("sr-rd-content-center").empty().append($img).append($label); }); break; case "blog.csdn.net": - $target.find( ".save_code" ).remove(); - $target.find( ".pre-numbering" ).remove(); - $target.find( "pre" ).removeAttr( "style" ).removeAttr( "class" ); - $target.find( "code" ).removeAttr( "style" ); - $target.find( ".dp-highlighter" ).map( ( index, item )=> { + $target.find(".save_code").remove(); + $target.find(".pre-numbering").remove(); + $target.find("pre").removeAttr("style").removeAttr("class"); + $target.find("code").removeAttr("style"); + $target.find(".dp-highlighter").map((index, item) => { $(item).find(".bar .tools").remove(); - if ( $(item).next().is( "pre" )) $(item).next().remove(); + if ($(item).next().is("pre")) $(item).next().remove(); }); break; case "news.sohu.com": - $target.find( ".conserve-photo" ).remove(); - $target.find( "table" ).addClass( "sr-rd-content-center" ); + $target.find(".conserve-photo").remove(); + $target.find("table").addClass("sr-rd-content-center"); break; case "qq.com": - $target.find( ".rv-root-v2, #backqqcom" ).remove(); + $target.find(".rv-root-v2, #backqqcom").remove(); break; case "azofreeware.com": - $target.find( "iframe" ).remove(); + $target.find("iframe").remove(); break; case "apprcn.com": - $target.find( "img" ).map( ( index, item ) => { + $target.find("img").map((index, item) => { const $target = $(item), - src = $target.attr( "src" ); - if ( src && src.includes( "Apprcn_Wechat_Small.jpeg" ) ) $target.parent().remove(); + src = $target.attr("src"); + if (src && src.includes("Apprcn_Wechat_Small.jpeg")) $target.parent().remove(); }); - $target.find( "a" ).map( ( index, item ) => { + $target.find("a").map((index, item) => { const $target = $(item), - text = $target.text(); - if ( text == "来自反斗软件" ) $target.parent().remove(); + text = $target.text(); + if (text == "来自反斗软件") $target.parent().remove(); }); break; case "tieba.baidu.com": - $target.find( ".BDE_Smiley" ).addClass( "sr-rd-content-nobeautify" ); - $target.find( ".replace_div" ).removeAttr( "class" ).removeAttr( "style" ); - $target.find( ".replace_tip" ).remove(); + $target.find(".BDE_Smiley").addClass("sr-rd-content-nobeautify"); + $target.find(".replace_div").removeAttr("class").removeAttr("style"); + $target.find(".replace_tip").remove(); break; case "question.zhihu.com": - $target.find( ".zu-edit-button" ).remove(); + $target.find(".zu-edit-button").remove(); break; case "chiphell.com": - $target.find( "img" ).map( ( index, item ) => { + $target.find("img").map((index, item) => { const $target = $(item), - $parent = $target.parent(), - src = $target.attr( "src" ); - if ( $parent.is( "ignore_js_op" )) $target.unwrap(); - if ( src && src.includes( "static/image/smiley" ) ) $target.addClass( "sr-rd-content-nobeautify" ); + $parent = $target.parent(), + src = $target.attr("src"); + if ($parent.is("ignore_js_op")) $target.unwrap(); + if (src && src.includes("static/image/smiley")) $target.addClass("sr-rd-content-nobeautify"); }); - $target.find( ".quote" ).remove(); + $target.find(".quote").remove(); break; case "jiemian.com": - $target.find( "script" ).remove(); + $target.find("script").remove(); break; case "36kr.com": - $target.find( ".load-html-img" ).removeAttr( "class" ); + $target.find(".load-html-img").removeAttr("class"); break; case "cnblogs.com": - $target.find( ".cnblogs_code" ).removeClass(); - $target.find( ".cnblogs_code_hide" ).removeClass().removeAttr( "style" ); - $target.find( ".cnblogs_code_toolbar" ).remove(); - $target.find( ".code_img_opened" ).remove(); - $target.find( ".code_img_closed" ).remove(); + $target.find(".cnblogs_code").removeClass(); + $target.find(".cnblogs_code_hide").removeClass().removeAttr("style"); + $target.find(".cnblogs_code_toolbar").remove(); + $target.find(".code_img_opened").remove(); + $target.find(".code_img_closed").remove(); break; case "news.cnblogs.com": - $target.find( ".topic_img" ).remove(); + $target.find(".topic_img").remove(); break; case "g-cores.com": - $target.find( ".swiper-slide-active" ).find( "img" ).map( ( index, item ) => { + $target.find(".swiper-slide-active").find("img").map((index, item) => { const $target = $(item); - $target.parent().parent().parent().parent().parent().parent().removeAttr( "class" ).removeAttr( "style" ).html($target); + $target.parent().parent().parent().parent().parent().parent().removeAttr("class").removeAttr("style").html($target); }); break; case "feng.com": - $target.find( "span" ).removeAttr( "style" ); + $target.find("span").removeAttr("style"); break; case "young.ifeng.com": - $target.find( "span" ).removeAttr( "style" ); + $target.find("span").removeAttr("style"); break; case "ftchinese.com": - $target.find( "script" ).remove(); + $target.find("script").remove(); break; case "segmentfault.com": - $target.find( ".widget-codetool" ).remove(); + $target.find(".widget-codetool").remove(); break; case "mp.weixin.qq.com": - $target.find( 'section[powered-by="xiumi.us"]' ).find( "img" ).map( ( index, item ) => { + $target.find('section[powered-by="xiumi.us"]').find("img").map((index, item) => { const $target = $(item), - src = $target.attr( "data-src" ); - $target.addClass( "sr-rd-content-nobeautify" ).attr( "src", src ); + src = $target.attr("data-src"); + $target.addClass("sr-rd-content-nobeautify").attr("src", src); }); break; - case "ruby-china.org": - $target.find( ".twemoji" ).remove(); + case "ruby-china.org": + $target.find(".twemoji").remove(); break; case "w3cplus.com": - $target.find( "iframe" ).addClass( "sr-rd-content-nobeautify" ); + $target.find("iframe").addClass("sr-rd-content-nobeautify"); break; case "zuojj.com": - $target.find( ".syntaxhighlighter .Brush" ).attr( "style", "font-size: .7em !important;" ) + $target.find(".syntaxhighlighter .Brush").attr("style", "font-size: .7em !important;") break; case "aotu.io": - $target.find( ".highlight table" ).map( ( index, item ) => { + $target.find(".highlight table").map((index, item) => { const $target = $(item), - $pre = $target.find( "pre" ), - $table = $target.find( "table" ); - $target.html( $pre[1] ); + $pre = $target.find("pre"), + $table = $target.find("table"); + $target.html($pre[1]); $table.unwrap(); }); - $target.find( "table" ).addClass( "sr-rd-content-center" ); + $target.find("table").addClass("sr-rd-content-center"); break; case "colobu.com": - $target.find( ".highlight table" ).map( ( index, item ) => { + $target.find(".highlight table").map((index, item) => { const $target = $(item), - $pre = $target.find( "pre" ); - $target.html( $pre[1] ); + $pre = $target.find("pre"); + $target.html($pre[1]); $target.unwrap(); }); break; case "hao.caibaojian.com": - $target.find( ".tlink" ).map( ( index, item ) => { + $target.find(".tlink").map((index, item) => { const $target = $(item); - $target.html( "" ); + $target.html(""); }); break; case "wkee.net": - $target.find( "script" ).remove(); + $target.find("script").remove(); break; case "linux.cn": - $target.find( "pre" ).attr( "style", "background-color: #161b20; background-image: none;" ); - $target.find( "code" ).attr( "style", "background-color: transparent; background-image: none;" ); + $target.find("pre").attr("style", "background-color: #161b20; background-image: none;"); + $target.find("code").attr("style", "background-color: transparent; background-image: none;"); break; case "zhuanlan.zhihu.com": - $target.find( "div[data-src]" ).map( ( index, item ) => { + $target.find("div[data-src]").map((index, item) => { const $target = $(item), - src = $target.attr( "data-src" ); - $target.replaceWith( `
` ); + src = $target.attr("data-src"); + $target.replaceWith(`
`); }); break; } @@ -314,14 +318,14 @@ async function specbeautify( name, $target ) { * @param {object} target object * @return {object} new target object */ -function clone( target ) { - return $.extend( true, {}, target ); +function clone(target) { + return $.extend(true, {}, target); } export { - getURI as GetURI, - findSitebyURL as Getsite, - specbeautify as Beautify, + getURI as GetURI, + findSitebyURL as Getsite, + specbeautify as Beautify, removeSpareTag as RemoveTag, - verify as Verify -} \ No newline at end of file + verify as Verify +} diff --git a/src/service/storage.js b/src/service/storage.js index 93d6983d3..af437bee6 100644 --- a/src/service/storage.js +++ b/src/service/storage.js @@ -1,9 +1,9 @@ -console.log( "=== simpread storage load ===" ) +console.log("=== simpread storage load ===") import "babel-polyfill"; -import * as st from 'site'; -import {browser} from 'browser'; -import {version} from 'version'; +import * as st from 'site'; +import { browser } from 'browser'; +import { version } from 'version'; /** * Read and Write Chrome storage @@ -13,62 +13,62 @@ import {version} from 'version'; const name = "simpread", remote = "http://ojec5ddd5.bkt.clouddn.com/website_list.json", - local = browser.extension.getURL( "website_list.json" ), - mode = { - focus : "focus", - read : "read", - option : "option", - unrdist : "unrdist", + local = browser.extension.getURL("website_list.json"), + mode = { + focus: "focus", + read: "read", + option: "option", + unrdist: "unrdist", }, - site = { - name : "", // only read mode - title : "", // only read mode - desc : "", // only read mode - exclude : [], - include : "", + site = { + name: "", // only read mode + title: "", // only read mode + desc: "", // only read mode + exclude: [], + include: "", }, - focus = { - version : "2016-12-29", - bgcolor : "rgba( 235, 235, 235, 0.9 )", - opacity : 90, - shortcuts : "A S", - sites : [], // e.g. [ "", site ] + focus = { + version: "2016-12-29", + bgcolor: "rgba( 235, 235, 235, 0.9 )", + opacity: 90, + shortcuts: "A S", + sites: [], // e.g. [ "", site ] }, - read = { - version : "2017-03-16", - shortcuts : "A A", - theme : "github", + read = { + version: "2017-03-16", + shortcuts: "A A", + theme: "github", fontfamily: "default", - fontsize : "", // default 62.5% - layout : "", // default 20% - sites : [] // e.g. [ "", site ] + fontsize: "", // default 62.5% + layout: "", // default 20% + sites: [] // e.g. [ "", site ] }, option = { - version : "2017-04-03", - create : "", - update : "", - focus : 0, - read : 0, + version: "2017-04-03", + create: "", + update: "", + focus: 0, + read: 0, }, unread = { - idx : 0, - create : "", - url : "", - title : "", - desc : "", + idx: 0, + create: "", + url: "", + title: "", + desc: "", }; -let current = {}, - curori = {}, - origin = {}, - sync = {}, +let current = {}, + curori = {}, + origin = {}, + sync = {}, simpread = { version, option, focus, read, - unrdist : [], - sites : [], + unrdist: [], + sites: [], }, rdstcode = -1; @@ -80,7 +80,7 @@ class Storage { * @return {object} simpread["option"] */ get option() { - return simpread[ mode.option ]; + return simpread[mode.option]; } /** @@ -89,7 +89,7 @@ class Storage { * @return {object} simpread["focus"] */ get focus() { - return simpread[ mode.focus ]; + return simpread[mode.focus]; } /** @@ -98,7 +98,7 @@ class Storage { * @return {object} simpread["read"] */ get read() { - return simpread[ mode.read ]; + return simpread[mode.read]; } /** @@ -125,7 +125,7 @@ class Storage { * @return {array} unread list */ get unrdist() { - return simpread[ mode.unrdist ]; + return simpread[mode.unrdist]; } /** @@ -151,16 +151,16 @@ class Storage { * * @param {function} callback */ - Read( callback ) { - browser.storage.local.get( [name], function( result ) { + Read(callback) { + browser.storage.local.get([name], function (result) { let firstload = true; - if ( result && !$.isEmptyObject( result )) { - simpread = result[name]; + if (result && !$.isEmptyObject(result)) { + simpread = result[name]; firstload = false; } - origin = clone( simpread ); - callback( firstload ); - console.log( "chrome storage read success!", simpread, origin, result ); + origin = clone(simpread); + callback(firstload); + console.log("chrome storage read success!", simpread, origin, result); }); } @@ -170,9 +170,9 @@ class Storage { * @param {function} callback * @param {object} new simpread data structure */ - Write( callback, new_val = undefined ) { - new_val && Object.keys( new_val ).forEach( key => simpread[ key ] = new_val[key] ); - save( callback ); + Write(callback, new_val = undefined) { + new_val && Object.keys(new_val).forEach(key => simpread[key] = new_val[key]); + save(callback); } /** @@ -183,25 +183,25 @@ class Storage { * * @param {string} @see mode */ - Getcur( key ) { - const [ url, sites ] = [ st.GetURI(), new Map( simpread[key].sites )]; - current = swap( simpread[key], {} ); - current.url = url; + Getcur(key) { + const [url, sites] = [st.GetURI(), new Map(simpread[key].sites)]; + current = swap(simpread[key], {}); + current.url = url; current.mode = key; - let arr = st.Getsite( new Map( simpread[key].sites ), url ); - arr ? setCode( key, 0 ) : setCode( key, 1 ); - !arr && ( arr = st.Getsite( new Map( simpread.sites ), url )); - if ( arr ) { + let arr = st.Getsite(new Map(simpread[key].sites), url); + arr ? setCode(key, 0) : setCode(key, 1); + !arr && (arr = st.Getsite(new Map(simpread.sites), url)); + if (arr) { current.site = arr[0]; - current.url = arr[1]; + current.url = arr[1]; } else { - sites.set( url, clone( site )); - current.site = sites.get( url ); - setCode( key, -1 ); + sites.set(url, clone(site)); + current.site = sites.get(url); + setCode(key, -1); } - curori = { ...current }; + curori = { ...current }; curori.site = { ...current.site }; - console.log( "current site object is ", current ) + console.log("current site object is ", current) } /** @@ -209,15 +209,15 @@ class Storage { * * @param {string} @see mode */ - Setcur( key ) { + Setcur(key) { const { code } = compare(); - if ( code != 0 ) { - if ( [ 2, 3 ].includes( code ) ) { - let idx = simpread[key].sites.findIndex( item => item[0] == current.url ); - idx == -1 && ( idx = simpread[key].sites.length ); - simpread[key].sites.splice( idx, 1, [ current.url, current.site ] ); + if (code != 0) { + if ([2, 3].includes(code)) { + let idx = simpread[key].sites.findIndex(item => item[0] == current.url); + idx == -1 && (idx = simpread[key].sites.length); + simpread[key].sites.splice(idx, 1, [current.url, current.site]); } - swap( current, simpread[key] ); + swap(current, simpread[key]); save(); } return code; @@ -228,10 +228,10 @@ class Storage { * * @param {string} @see mode */ - VerifyCur( type ) { - return ( current.mode && current.mode != type ) || - ( current.url && current.url != st.GetURI() ) || - $.isEmptyObject( current ); + VerifyCur(type) { + return (current.mode && current.mode != type) || + (current.url && current.url != st.GetURI()) || + $.isEmptyObject(current); } /** @@ -240,25 +240,33 @@ class Storage { * @param {string} url, e.g. chrome-extension://xxxx/website_list.json or http://xxxx.xx/website_list.json * @return {function} callback, param1: object; param2: error */ - async GetNewsites( type, callback ) { + async GetNewsites(type, callback) { try { - const url = type === "remote" ? remote : local, - response = await fetch( url + "?_=" + Math.round(+new Date()) ), - sites = await response.json(), - len = simpread.sites.length; - let [ count, forced ] = [ 0, 0 ]; - if ( len == 0 ) { - simpread.sites = formatSites( sites ); - count = simpread.sites.length; + const url = type === "remote" ? remote : local, + response = await fetch(url + "?_=" + Math.round(+new Date())), + sites = await response.json(), + len = simpread.sites.length; + let [count, forced] = [0, 0]; + sites.sites.push({ + "name": "github.issue", + "url": "https://github.com/*/*/issues/*", + "title": "[[{$('h1.gh-header-title .js-issue-title').text()}]]", + "desc": "", + "include": "[[{$('#discussion_bucket table td.js-comment-body').html()}]]", + "exclude": "" + }) + if (len == 0) { + simpread.sites = formatSites(sites); + count = simpread.sites.length; save(); } - else if ( { count, forced } = addsites( formatSites( sites )), count > 0 || forced > 0 ) { + else if ({ count, forced } = addsites(formatSites(sites)), count > 0 || forced > 0) { save(); } - callback && callback( { count, forced }, undefined ); - } catch ( error ) { - console.error( error ); - callback && callback( {}, error ); + callback && callback({ count, forced }, undefined); + } catch (error) { + console.error(error); + callback && callback({}, error); } } @@ -268,27 +276,27 @@ class Storage { * @param {string} include: set, get * @param {function} callback */ - Sync( state, callback ) { - if ( state == "set" ) { + Sync(state, callback) { + if (state == "set") { sync = { ...simpread }; sync.option.update = now(); delete sync.sites; - browser.storage.sync.set( { [name] : sync }, () => { - console.log( "chrome storage sync[set] success!" ) + browser.storage.sync.set({ [name]: sync }, () => { + console.log("chrome storage sync[set] success!") simpread.option.update = sync.option.update; - save( callback( sync.option.update )); + save(callback(sync.option.update)); }); } else { - browser.storage.sync.get( [name] , result => { - console.log( "chrome storage sync[get] success!", result, simpread ) + browser.storage.sync.get([name], result => { + console.log("chrome storage sync[get] success!", result, simpread) let success = false; - if ( result && !$.isEmptyObject( result )) { + if (result && !$.isEmptyObject(result)) { success = true; - Object.keys( mode ).forEach( key => { - simpread[ key ] = result[ name ][ key ]; + Object.keys(mode).forEach(key => { + simpread[key] = result[name][key]; }); } - callback( success ); + callback(success); }); } } @@ -298,11 +306,11 @@ class Storage { * * @param {string} include: create, focus, read */ - Statistics( type ) { - if ( type == "create" ) { + Statistics(type) { + if (type == "create") { simpread.option.create = now(); } else { - simpread.option[ type ] = simpread.option[ type ] + 1; + simpread.option[type] = simpread.option[type] + 1; } save(); } @@ -314,23 +322,23 @@ class Storage { * @param {any} include: object( @see unread ) or index * @param {function} callback */ - UnRead( type, args, callback ) { + UnRead(type, args, callback) { let success = true; - switch ( type ) { + switch (type) { case "add": const len = simpread.unrdist.length; args.create = now(); args.idx = len > 0 ? simpread.unrdist[0].idx + 1 : 0; - simpread.unrdist.findIndex( item => item.url == args.url ) == -1 ? - simpread.unrdist.splice( 0, 0, args ) : success = false; + simpread.unrdist.findIndex(item => item.url == args.url) == -1 ? + simpread.unrdist.splice(0, 0, args) : success = false; break; case "remove": - const idx = simpread.unrdist.findIndex( item => item.idx == args ); - idx != -1 && simpread.unrdist.splice( idx, 1 ); - idx == -1 && ( success = false ); + const idx = simpread.unrdist.findIndex(item => item.idx == args); + idx != -1 && simpread.unrdist.splice(idx, 1); + idx == -1 && (success = false); break; } - callback && save( callback( success ) ); + callback && save(callback(success)); } /** @@ -340,40 +348,40 @@ class Storage { * @return {object} option: { code: 0|-1|-2, keys: [ "bgcolor", "layout" ] } * code: 0: valid success; -1: field name failed; -2: site field name failed; */ - Verify( data = undefined ) { + Verify(data = undefined) { const pendding = data ? { ...data } : { simpread }, - valid = ( value, source ) => { - let result = { code: 0, keys: [] }, target = pendding[ value ]; - if ( Object.keys( target ).length !== Object.keys( source ).length ) { - result.code = -1; - } else { - Object.keys( target ).forEach( key => { - if ( !Object.keys( source ).includes( key ) || - ( key != "sites" && value != "option" && target[key] == "" )) { - result.keys.push( key ); - } - if ( key == "sites" ) { - target.sites.forEach( items => { - if ( Object.keys( items[1] ).length != Object.keys( site ).length ) { - result.code = -2; - } else { - Object.keys( items[1] ).forEach( key => { - ( !Object.keys( site ).includes( key ) ) && result.keys.push( `site::${key}` ); - }); - } - }); - } - }); - result.keys.length > 0 && result.code == 0 && ( result.code = -3 ); + valid = (value, source) => { + let result = { code: 0, keys: [] }, target = pendding[value]; + if (Object.keys(target).length !== Object.keys(source).length) { + result.code = -1; + } else { + Object.keys(target).forEach(key => { + if (!Object.keys(source).includes(key) || + (key != "sites" && value != "option" && target[key] == "")) { + result.keys.push(key); + } + if (key == "sites") { + target.sites.forEach(items => { + if (Object.keys(items[1]).length != Object.keys(site).length) { + result.code = -2; + } else { + Object.keys(items[1]).forEach(key => { + (!Object.keys(site).includes(key)) && result.keys.push(`site::${key}`); + }); + } + }); + } + }); + result.keys.length > 0 && result.code == 0 && (result.code = -3); + } + return result; } - return result; - } - let opt = valid( "option", option ), - focu = valid( "focus", focus ), - rd = valid( "read", read ); + let opt = valid("option", option), + focu = valid("focus", focus), + rd = valid("read", read); - console.log( "storage.Verify() result ", opt, focu, rd ) + console.log("storage.Verify() result ", opt, focu, rd) return { option: opt, focus: focu, read: rd }; } @@ -382,9 +390,9 @@ class Storage { * * @param {string} @see mode */ - Restore( key ) { - simpread[key] = clone( origin[key] ); - this.Getcur( key ); + Restore(key) { + simpread[key] = clone(origin[key]); + this.Getcur(key); } /** @@ -393,12 +401,12 @@ class Storage { * @param {string} include: local remote all * @param {function} callback */ - Clear( state, callback ) { + Clear(state, callback) { let code = 2; - state == "local" && ( code = 0 ); - state == "remote" && ( code = 1 ); - ( code == 0 || code == 2 ) && browser.storage.local.clear( callback ); - ( code == 1 || code == 2 ) && browser.storage.sync.clear( callback ); + state == "local" && (code = 0); + state == "remote" && (code = 1); + (code == 0 || code == 2) && browser.storage.local.clear(callback); + (code == 1 || code == 2) && browser.storage.sync.clear(callback); } } @@ -409,9 +417,9 @@ class Storage { * @param {object} source origin * @param {object} target origin */ -function swap( source, target ) { - for ( const key of Object.keys( source ) ) { - if ( ![ "site", "sites", "version", "url", "mode" ].includes( key ) ) { +function swap(source, target) { + for (const key of Object.keys(source)) { + if (!["site", "sites", "version", "url", "mode"].includes(key)) { target[key] = source[key]; } } @@ -424,8 +432,8 @@ function swap( source, target ) { * @param {object} target object * @return {object} new target object */ -function clone( target ) { - return $.extend( true, {}, target ); +function clone(target) { + return $.extend(true, {}, target); } /** @@ -434,14 +442,14 @@ function clone( target ) { * @param {object} sites.[array] * @return {array} foramat e.g. [[ , object ],[ , object ]] */ -function formatSites( result ) { +function formatSites(result) { const format = new Map(); - for ( let site of result.sites ) { + for (let site of result.sites) { const url = site.url; delete site.url; - format.set( url, site ); + format.set(url, site); } - return [ ...format ]; + return [...format]; } /** @@ -450,17 +458,17 @@ function formatSites( result ) { * @param {array} new sites from local or remote * @return {object} count: new sites; forced: update sites */ -function addsites( sites ) { - const update = new Map( simpread.sites ), - urls = [ ...update.keys() ]; - let [ count, forced ] = [ 0, 0 ]; - sites.map( ( site ) => { - if ( !urls.includes( site[0] ) ) { - simpread.sites.push([ site[0], site[1] ]); +function addsites(sites) { + const update = new Map(simpread.sites), + urls = [...update.keys()]; + let [count, forced] = [0, 0]; + sites.map((site) => { + if (!urls.includes(site[0])) { + simpread.sites.push([site[0], site[1]]); count++; - } else if ( urls.includes( site[0] ) && site[1].override ) { - update.set( site[0], site[1] ); - simpread.sites = [ ...update ]; + } else if (urls.includes(site[0]) && site[1].override) { + update.set(site[0], site[1]); + simpread.sites = [...update]; forced++; } }); @@ -470,11 +478,11 @@ function addsites( sites ) { /** * Call chrome storage set */ -function save( callback ) { - browser.storage.local.set( { [name] : simpread }, function() { - console.log( "chrome storage save success!", simpread ); - origin = clone( simpread ); - curori = { ...current }; +function save(callback) { + browser.storage.local.set({ [name]: simpread }, function () { + console.log("chrome storage save success!", simpread); + origin = clone(simpread); + curori = { ...current }; curori.site = { ...current.site }; callback && callback(); }); @@ -486,8 +494,8 @@ function save( callback ) { * @param {string} mode type * @param {int} -1: not found; 0: simpread.read.sites; 1: simpread.sites */ -function setCode( type, value ) { - if ( type == mode.read ) rdstcode = value; +function setCode(type, value) { + if (type == mode.read) rdstcode = value; } /** @@ -499,20 +507,20 @@ function setCode( type, value ) { */ function compare() { let key, code = 0, changed = [], site_changed = false; - for ( key of Object.keys( curori ) ) { - if ( typeof curori[key] == "string" && curori[key] != current[key] ) { - changed.push( key ); + for (key of Object.keys(curori)) { + if (typeof curori[key] == "string" && curori[key] != current[key]) { + changed.push(key); code = 1; } } - for ( key of Object.keys( curori.site ) ) { - if ( ( typeof curori.site[key] == "object" && curori.site[key].join( "" ) != current.site[key].join( "" ) ) || curori.site[key] != current.site[key] ) { - changed.push( key ); + for (key of Object.keys(curori.site)) { + if ((typeof curori.site[key] == "object" && curori.site[key].join("") != current.site[key].join("")) || curori.site[key] != current.site[key]) { + changed.push(key); site_changed = true; } } - site_changed && ( code = code + 2 ); - console.log( "current changed state is ", code, changed ); + site_changed && (code = code + 2); + console.log("current changed state is ", code, changed); return { code, changed }; } @@ -522,16 +530,16 @@ function compare() { * @return {string} return now, e.g. 2017年04月03日 11:43:53 */ function now() { - const date = new Date(), - format = value => value = value < 10 ? "0" + value : value; - return date.getFullYear() + "年" + format( date.getUTCMonth() + 1 ) + "月" + format( date.getUTCDate() ) + "日 " + format( date.getHours() ) + ":" + format( date.getMinutes() ) + ":" + format( date.getSeconds() ); + const date = new Date(), + format = value => value = value < 10 ? "0" + value : value; + return date.getFullYear() + "年" + format(date.getUTCMonth() + 1) + "月" + format(date.getUTCDate()) + "日 " + format(date.getHours()) + ":" + format(date.getMinutes()) + ":" + format(date.getSeconds()); } const storage = new Storage(); export { storage, - mode as STORAGE_MODE, + mode as STORAGE_MODE, clone as Clone, - now as Now, -}; \ No newline at end of file + now as Now, +}; diff --git a/webpack.config.js b/webpack.config.js index c58901f9f..7d6f89c11 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -187,6 +187,7 @@ const webpack = require( 'webpack' ), resolve: { alias : { + minimatch : __dirname + '/src/node_modules/minimatch/minimatch.js', jquery : __dirname + '/src/vender/jquery-2.1.1.min.js', mousetrap : __dirname + '/src/vender/mousetrap.min.js', pangu : __dirname + '/src/vender/pangu.min.js', @@ -246,4 +247,4 @@ const webpack = require( 'webpack' ), }; -module.exports = config; \ No newline at end of file +module.exports = config; From 786571e520fbbb401131d03d88d0ac163d01f499 Mon Sep 17 00:00:00 2001 From: ksky521 Date: Thu, 15 Jun 2017 15:40:03 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E5=88=A0=E9=99=A4github=20issue=20demo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/service/storage.js | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/service/storage.js b/src/service/storage.js index af437bee6..072082039 100644 --- a/src/service/storage.js +++ b/src/service/storage.js @@ -247,14 +247,7 @@ class Storage { sites = await response.json(), len = simpread.sites.length; let [count, forced] = [0, 0]; - sites.sites.push({ - "name": "github.issue", - "url": "https://github.com/*/*/issues/*", - "title": "[[{$('h1.gh-header-title .js-issue-title').text()}]]", - "desc": "", - "include": "[[{$('#discussion_bucket table td.js-comment-body').html()}]]", - "exclude": "" - }) + if (len == 0) { simpread.sites = formatSites(sites); count = simpread.sites.length; From 23a1ec3fd870ae01c23c4a5b480cc743efeda04c Mon Sep 17 00:00:00 2001 From: ksky521 Date: Thu, 15 Jun 2017 15:46:39 +0800 Subject: [PATCH 3/6] =?UTF-8?q?minimatch=E5=BC=95=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- webpack.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webpack.config.js b/webpack.config.js index 7d6f89c11..1323a7eb3 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -187,7 +187,7 @@ const webpack = require( 'webpack' ), resolve: { alias : { - minimatch : __dirname + '/src/node_modules/minimatch/minimatch.js', + minimatch : __dirname + '/node_modules/minimatch/minimatch.js', jquery : __dirname + '/src/vender/jquery-2.1.1.min.js', mousetrap : __dirname + '/src/vender/mousetrap.min.js', pangu : __dirname + '/src/vender/pangu.min.js', From 9a5ab6b29c2cfcfa9a5d521e05ffe7f7c76b2088 Mon Sep 17 00:00:00 2001 From: ksky521 Date: Thu, 15 Jun 2017 18:09:45 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E5=A2=9E=E5=8A=A0markdown=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 1 + src/assets/css/simpread.css | 9 ++++++++ src/assets/images/markdown_icon.png | Bin 0 -> 374 bytes src/module/common/exclude.jsx | 2 +- src/read/controlbar.jsx | 2 +- src/read/read.jsx | 33 ++++++++++++++++++++++++++-- src/service/config.js | 7 +++++- webpack.config.js | 6 +++++ 8 files changed, 55 insertions(+), 5 deletions(-) create mode 100644 src/assets/images/markdown_icon.png diff --git a/package.json b/package.json index f25972cf8..28aa7ba3c 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "react": "^0.14.8", "react-dom": "^0.14.8", "style-loader": "^0.13.1", + "to-markdown": "^3.0.4", "url-loader": "^0.5.7", "webpack": "^1.14.0", "webpack-dev-server": "^1.16.2" diff --git a/src/assets/css/simpread.css b/src/assets/css/simpread.css index 4fa077d1f..488f1c2e4 100644 --- a/src/assets/css/simpread.css +++ b/src/assets/css/simpread.css @@ -196,3 +196,12 @@ sr-rd-footer a:active { color: #787878; text-decoration: underline; } +sr-rd-markdown{ + width: 100%; +} +sr-rd-markdown textarea{ + font-size: 16px; + min-width: 800px; + height: 650px; + border: 1px solid #ccc; +} diff --git a/src/assets/images/markdown_icon.png b/src/assets/images/markdown_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..d2bd862fcdb281032b255308133ee15c536e77e7 GIT binary patch literal 374 zcmV-+0g3*JP)Mww--zO9u3kEL7*5S*HU~p7ON?U59f!ekUo1P0#on4h9ATgMq=oKhbBk UAp{RI)&Kwi07*qoM6N<$f{o^$kN^Mx literal 0 HcmV?d00001 diff --git a/src/module/common/exclude.jsx b/src/module/common/exclude.jsx index df31f09b5..d31b77417 100644 --- a/src/module/common/exclude.jsx +++ b/src/module/common/exclude.jsx @@ -34,7 +34,7 @@ export default class Exclude extends React.Component { rows={ this.props.rows } placeholder="默认为空,每行一个,支持: Html标签, {}, '', //, [] 等,详细请看站点编辑器。" floatingtext="隐藏列表" - value={ this.props.exclude.join( "\n" ) } + value={ (this.props.exclude||[]).join( "\n" ) } errortext={ this.state.error } onChange={ ()=>this.changeExclude() } /> diff --git a/src/read/controlbar.jsx b/src/read/controlbar.jsx index bcc5ce0eb..41c1aaa42 100644 --- a/src/read/controlbar.jsx +++ b/src/read/controlbar.jsx @@ -46,7 +46,7 @@ export default class ReadCtlbar extends React.Component { } browser.runtime.sendMessage( msg.Add( msg.MESSAGE_ACTION.new_tab, { url })); break; - case [ "exit", "setting", "save" ].includes( type ): + case [ "exit", "setting", "save", "markdown" ].includes( type ): this.props.onAction && this.props.onAction( type ); break; case [ "up", "down" ].includes( type ): diff --git a/src/read/read.jsx b/src/read/read.jsx index ec6ab7593..ef4b899d5 100644 --- a/src/read/read.jsx +++ b/src/read/read.jsx @@ -1,5 +1,5 @@ console.log( "=== simpread read load ===" ) - +import toMarkdown from 'to-markdown'; import pangu from 'pangu'; import ProgressBar from 'schedule'; import ReadCtlbar from 'readctlbar'; @@ -36,11 +36,18 @@ const Footer = () => { ) } -class Read extends React.Component { +class Read extends React.Component { + constructor(props) { + super(props); + this.state = {isShowMarkdown: false}; + } componentWillMount() { $( "body" ).addClass( "simpread-hidden" ); th.Change( this.props.read.theme ); + let {include} = this.props.wrapper; + //转换为markdown + this.props.markdown = toMarkdown(include, { gfm: true }); } async componentDidMount() { @@ -105,6 +112,25 @@ class Read extends React.Component { storage.current[type]=value; storage.Setcur( storage.current.mode ); break; + case "markdown": + if(this.props.markdown){ + if(!this.state.isShowMarkdown){ + this.setState({ + isShowMarkdown: true + }) + $('sr-rd-content,sr-rd-footer').hide(); + $('sr-rd-markdown textarea').val(this.props.markdown).show(); + }else{ + this.setState({ + isShowMarkdown: false + }) + $('sr-rd-content,sr-rd-footer').show(); + $('sr-rd-markdown textarea').hide(); + } + }else{ + new Notify().Render( 1, '转换markdown失败!' ); + } + break; } } @@ -125,6 +151,9 @@ class Read extends React.Component { { this.props.wrapper.title } { this.props.wrapper.desc } + + +