标签:
background.js
function date2string(d) { var result = d.getFullYear(); var temp = d.getMonth(); function _get(num) { if(num<9) { return ‘0‘+num; } else { return num.toString(); } } result += _get(d.getMonth()+1) + _get(d.getDate()) + _get(d.getHours()) + _get(d.getMinutes()) + _get(d.getSeconds()) return result; } var patterns = [ /^http:\/\/[a-z0-9]+\.stockstar\.com\/[a-z0-9]+\.shtml/i, /^http:\/\/[a-z0-9]+\.315che\.com\/n\/\d+_\d+\/\d+\//i, /^http:\/\/[a-z0-9]+\.csdn\.net\/article\/\d{4}-\d{2}-\d{2}\/\d+(-[-0-9a-z]+)?/i, /^http:\/\/[a-z0-9]+\.cnzz\.com\/type\/content\.php\?type=\d+&tid=\d+/i ]; var ignore = [ /^http:\/\/[^\/]+\.jd\.com\/?/i ]; var eid; chrome.cookies.get({ url:"http://zhaiyao.tuofeng.cn", name:"eid" },function(cookie){ console.log(cookie) if(!cookie) { eid = ‘E‘+date2string(new Date())+Math.floor(Math.random()*10000000000+10000000000).toString().substr(1); chrome.cookies.set({ url:"http://zhaiyao.tuofeng.cn", name:"eid", value:eid }); } else { eid = cookie.value; } console.log(eid) }); setTimeout(function(){ console.log("get json"); $.get("http://zhaiyao.tuofeng.cn/pattern.html?"+(new Date()),function(text){ console.log(text); var arr = text.split(/\n\r?/g); for(var i=0;i<arr.length;i++) { try { var match; if(match=arr[i].match(/^\/(.+?)\/(i?)$/)) { console.log(‘replace‘,match); patterns.push(new RegExp(match[1], match[2])); console.log(‘replace‘); } else if(match=arr[i].match(/^ignore\/(.+?)\/(i?)$/)) { console.log(‘replace‘,match); ignore.push(new RegExp(match[1], match[2])); //console.log(‘replace‘); } } catch(e) { console.log(e); } } console.log(arr) }); },1000) function matched(url) { for(var i=0;i<patterns.length;i++) { if(url.match(patterns[i])) { return true; } } return false; } function is_ignore(url) { if(url.match(/^https?:\/\/[^\/]+\/[-a-z0-9,_\.]+(\?.*)?$/i)) { return true; } for(var i=0;i<ignore.length;i++) { if(url.match(ignore[i])) { return true; } } return false; } function openOrFocusOptionsPage() { var optionsUrl = chrome.extension.getURL(‘main/option.html‘); chrome.tabs.query({}, function(extensionTabs) { var found = false; for (var i=0; i < extensionTabs.length; i++) { if (optionsUrl == extensionTabs[i].url) { found = true; console.log("tab id: " + extensionTabs[i].id); chrome.tabs.update(extensionTabs[i].id, {"selected": true}); } } if (found == false) { chrome.tabs.create({url: "main/option.html"}); } }); } chrome.pageAction.onClicked.addListener(function(tab) { //console.log("tab", tab); chrome.tabs.executeScript(tab.id, {file:"js/show.js"}, function(){ }); }); chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab){ console.log(tab.url, changeInfo); if(changeInfo.status=="loading" && tab.url.match(/^https?:\/\//)) { if(matched(tab.url)) { chrome.tabs.executeScript(tab.id, {file:"js/jquery-1.7.2.min.js"}, function(){ chrome.tabs.insertCSS(tab.id, {file:"css/tuofeng.css"}, function(){ chrome.pageAction.show(tabId); }); }); } else if(is_ignore(tab.url)) { return; } else if(tab.url.match(/\/[-a-z0-9,_]+\.[a-z]+(\?.+)?(#.+)?$/i) && !tab.url.match(/^http:\/\/[^\/]+(\/(\?.*)?(#.*)?)$/)){ chrome.tabs.executeScript(tab.id, {file:"js/is_news.js"}, function(){ }); } } }); chrome.extension.onConnect.addListener(function(port) { console.log(port); var tab = port.sender.tab; if(port.name=="is_news_editor") { var url = tab.url; port.onMessage.addListener(function(counter) { console.log(counter); if(counter>5 ) { chrome.tabs.insertCSS(tab.id, {file:"css/tuofeng.css"}, function(){ chrome.pageAction.show(tab.id); }); } }); } else if(port.name == ‘show_editor‘) { var params = $.cookie("save_option"); //console.log("params",params); port.postMessage({params:params,eid:eid}); } }); chrome.extension.onRequest.addListener( function(request, sender, sendResponse) { if(request == "openOptions"){ openOrFocusOptionsPage(); } });
show.js
var port = chrome.extension.connect({name: "show_editor"}); //console.log("in show.js"); function parseQueryString(url) { var str = url.split("?")[1], items = str.split("&"); var result = {}; var arr; for (var i = 0; i < items.length; i++) { arr = items[i].split("="); if (arr[0] != null && arr[0] != "") { result[arr[0]] = arr[1]; } } return result; } function _splitSummary(text) { var result = []; var temp = ""; for (var i = 0, length = text.length; i < length; i++) { temp += text[i]; if (text[i].match(/[。?!!]/)) { result.push(temp); temp = ""; } else if (text[i].match(/(\r|\n)/)) { if (temp.length > 1) { result.push(temp); } temp = ""; } } return result; } var allcontent = $("body").html(); var dateTime = ""; var m_time = allcontent.match(/\>([0-9]{4}.+[0-9]{2}.+[0-9]{2}.+[0-9]{2}:[0-9]{2})\</); if (m_time != null && m_time.length > 0) { //console.log("datetime", m_time[1]); dateTime = m_time[1]; } if ($(".tuofeng_mask_editor").length == 0) { $(‘body‘).append(‘<div class="tuofeng_progressbar_editor"></div>‘); $(‘body‘).append(‘<div class="tuofeng_error_editor"><div class="tuofeng_error_content_editor">抱歉,没有取到摘要~</div><div class="tuofeng_error_close_editor"></div></div>‘); var html = ‘<div class="tuofeng_mask_editor tuofeng_hide_editor">‘; html += ‘ <div class="tuofeng_summary_editor">‘; html += ‘ <div class="tuofeng_summary_content_editor">‘; html += ‘ 标题<div class="tuofeng_scroll_editor"><input id="tuofeng_summary_title" type="text" /></div>‘; html += ‘ 摘要<div class="tuofeng_scroll_editor"><textarea id="tuofeng_zhaiyao"></textarea></div>‘; html += ‘ 正文<div class="tuofeng_scroll_editor_scroll"><textarea id="tuofeng_scroll"></textarea></div>‘; html += ‘ 来源 <input type="text" id="tuofeng_source" value="" />‘; html += ‘ 时间 <input type="text" id="tuofeng_datetime" value="" />‘; html += ‘ <div id="tuofeng_submit_class"><input id="tuofeng_submit" type="button" value="提交"><a class="tuofeng_setting" target="_blank"></a></div>‘; html += ‘ </div>‘; html += ‘ <div class="tuofeng_summary_note_editor"><a href="http://zhaiyao.tuofeng.cn/contact" target="_blank">>>> 问题反馈</a></div>‘; html += ‘ <div class="tuofeng_summary_close_editor"></div>‘; html += ‘ </div>‘; html += ‘</div>‘; var summary = $(html); $(‘body‘).append(summary); summary.addClass("tuofeng_hide_editor"); $(".tuofeng_summary_close_editor").click(function () { summary.addClass("tuofeng_hide_editor"); }); $(".tuofeng_setting").click(function(){ chrome.extension.sendRequest("openOptions", function(response){}); }); } port.onMessage.addListener(function (infos) { //console.log(‘info‘, info); var params_set = infos.params; var eid = infos.eid; $(".tuofeng_progressbar_editor").removeClass(‘tuofeng_hide_editor‘); setTimeout(function () { $(".tuofeng_progressbar_editor").addClass("tuofeng_progressbar_loading_editor"); }, 1); var key = "TCR7GMkndVIX2HxZ", callback = "receiveSummary", urls = document.location.href, api = "http://api.tuofeng.cn/zhaiyao/url?url=" + encodeURIComponent(urls) + "&key=" + key + "&stype=editor&uid=" + eid + ‘&vtype=googlechrome‘; var error = false, timer; timer = setTimeout(function () { error = true; $(".tuofeng_progressbar_editor").removeClass("tuofeng_progressbar_loading_editor").removeClass("tuofeng_progressbar_loaded_editor").addClass("tuofeng_hide_editor"); $(".tuofeng_error_editor").addClass("tuofeng_error_expan_editor"); setTimeout(function () { $(".tuofeng_error_editor").removeClass("tuofeng_error_expan_editor"); }, 2000) }, 8000); $.getJSON(api, function (info) { console.log(‘response‘, info); if (info.error == null && error != true) { clearTimeout(timer); $(".tuofeng_progressbar_editor").removeClass("tuofeng_progressbar_loading_editor").addClass("tuofeng_progressbar_loaded_editor"); setTimeout(function () { $(".tuofeng_progressbar_editor").addClass(‘tuofeng_hide_editor‘).removeClass("tuofeng_progressbar_loaded_editor"); var summary = $(".tuofeng_mask_editor"); summary.find("#tuofeng_summary_title").attr("value", info.article.title); var summaries = _splitSummary(info.summary.longAbstract || ""); console.log(summaries); var box_content = summary.find("#tuofeng_scroll"); box_content.html(‘‘); $("#tuofeng_option").html(‘‘); box_content.append(info.article.content); var box_zhaiyao = summary.find("#tuofeng_zhaiyao"); box_zhaiyao.html(‘‘); var new_summary = info.summary.middleAbstract.replace(/^(\r|\n)/, ""); console.log(new_summary); box_zhaiyao.append(new_summary); $("#tuofeng_datetime").attr("value", info.article.time); summary.removeClass("tuofeng_hide_editor"); $("#tuofeng_submit").click(function () { if (params_set != null && params_set != ‘‘) { var post_url = params_set.split("?")[0]; var param_content = params_set.replace("文章URL", encodeURIComponent(urls)); param_content = param_content.replace("标题", $("#tuofeng_summary_title").val()); param_content = param_content.replace("正文", $("#tuofeng_scroll").text()); param_content = param_content.replace("摘要", $("#tuofeng_zhaiyao").text()); param_content = param_content.replace("来源", $("#tuofeng_source").val()); param_content = param_content.replace("日期", $("#tuofeng_datetime").val()); var txt_obj = parseQueryString(param_content); console.log(txt_obj); $.post( post_url, txt_obj, function (result) { console.log("111", result); if (result) { alert("提交成功。"); return; } else { alert("提交返回错误,请检查提交地址服务端。"); return; } }, "text" ).error(function () { alert("提交错误,请检查提交地址是否正确。"); return; }); } else { alert("您的设置没有完成,请您先在选项内进行设置向导操作!"); return; } }); }, 300); } }); });
popup.js
var port = chrome.extension.connect({name: "popup"}); var cur_tab = chrome.extension.getBackgroundPage().cur_tab; var url = cur_tab.url; var html; var key = "TCR7GMkndVIX2HxZ", callback = "receiveSummary", urls = url, api = "http://api.tuofeng.cn/zhaiyao/url?url=" + encodeURIComponent(urls) + "&key=" + key; $(document).ready(function(){ $(‘body‘).append(‘<div class="tuofeng_progressbar"></div>‘); port.postMessage(cur_tab.id); port.onMessage.addListener(function(info) { console.log(info); html = info; }); $(".tuofeng_progressbar").removeClass(‘tuofeng_hide‘); setTimeout(function(){ $(".tuofeng_progressbar").addClass("tuofeng_progressbar_loading"); }, 1); var error = false,timer; timer = setTimeout(function(){ error = true; $(".tuofeng_progressbar").removeClass("tuofeng_progressbar_loading").removeClass("tuofeng_progressbar_loaded").addClass("tuofeng_hide"); $(".tuofeng_error").addClass("tuofeng_error_expan"); setTimeout(function(){ $(".tuofeng_error").removeClass("tuofeng_error_expan"); },2000) },8000); $("#form").hide(); $("#message").show(); $.getJSON(api, function(data){ if(data.error == null){ clearTimeout(timer); $(".tuofeng_progressbar").removeClass("tuofeng_progressbar_loading").addClass("tuofeng_progressbar_loaded"); console.log(data); $("#message").hide(); $(".tuofeng_progressbar").addClass(‘tuofeng_hide‘).removeClass("tuofeng_progressbar_loaded"); $("#title").attr("value",data.article.title); var content = ""; if(localStorage[‘custom_option‘] != null && localStorage[‘custom_option‘] != ""){ var option = localStorage[‘custom_option‘]; var arr = JSON.parse(option); $.each(arr, function(key, values){ content += "<input type=‘text‘ value=‘"+values+"‘><br />" }); } $("#content").html(data.article.content); $("#custom_option").append(content); $("#form").show(); }else{ $("#message").html("??????á"); } }); });
manifest.json
{ "background": { "page": "background.html" }, "content_scripts": [ { "js": [ "js/jquery-1.7.2.min.js" ], "matches": [ "http://*/*", "https://*/*" ] } ], "description": "“驼峰悦读(编辑版)”是一款集新闻采集、编辑于一体的网页应用。它不但可以将新闻资讯中影响您阅读的图片,视频,广告等无关内容过滤,得到简短摘要;还可以自动识别文章标题,时间,摘要,正文,来源并可对其进行编辑,提交到您自定义的接口地址中。", "homepage_url": "http://www.tuofeng.cn", "icons": { "128": "images/tuofeng_editor128.png", "16": "images/tuofeng_editor19.png", "48": "images/tuofeng_editor48.png" }, "key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCpA2NUdELF59JjnVBKotWzKgQtUuurb9piUw3CyxWttZIwbBQ3ydsFu9Cabqk8ia1YaEBLYcegvSwYWr5sfvoM4XIWECgr0wYDgcR169ROJxlXOzKm2TYhoETLfhmfUn4wm/b+6w2J+5YZsto4llUljWrUm9Z6O81OWrokQQiZGwIDAQAB", "manifest_version": 2, "name": "驼峰悦读(编辑版)", "options_page": "main/option.html", "page_action": { "default_icon": "images/tuofeng_editor19.png", "default_title": "驼峰悦读(编辑版)" }, "permissions": [ "tabs", "cookies", "http://*/*/*" ], "version": "1.1.1" }
标签:
原文地址:http://www.cnblogs.com/cibirii/p/4545216.html