123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506 |
- /**
- * FE-Helper后台运行程序
- * @author [email protected]
- */
- var BgPageInstance = (function () {
- //各种元素的就绪情况
- var _readyState = {
- css: false,
- js: false,
- html: true,
- allDone: false
- };
- /**
- * 文本格式,可以设置一个图标和标题
- * @param {Object} options
- * @config {string} type notification的类型,可选值:html、text
- * @config {string} icon 图标
- * @config {string} title 标题
- * @config {string} message 内容
- */
- var notifyText = function (options) {
- if (!window.Notification) {
- return;
- }
- if (!options.icon) {
- options.icon = "static/img/fe-48.png";
- }
- if (!options.title) {
- options.title = "温馨提示";
- }
- chrome.notifications.create('', {
- type: 'basic',
- title: options.title,
- iconUrl: chrome.runtime.getURL(options.icon),
- message: options.message
- });
- };
- //侦测的interval
- var _detectInterval = null;
- //侦测就绪情况
- var _detectReadyState = function () {
- _detectInterval = window.setInterval(function () {
- if (_readyState.css && _readyState.js && _readyState.html) {
- _readyState.allDone = true;
- window.clearInterval(_detectInterval);
- }
- }, 100);
- };
- /**
- * 执行前端FCPHelper检测
- */
- var _doFcpDetect = function (tab) {
- //所有元素都准备就绪
- if (_readyState.allDone) {
- chrome.tabs.sendMessage(tab.id, {
- type: MSG_TYPE.BROWSER_CLICKED,
- event: MSG_TYPE.FCP_HELPER_DETECT
- });
- } else {
- //显示桌面提醒
- notifyText({
- message: "正在准备数据,请稍等..."
- });
- }
- };
- /**
- * 执行栅格检测
- */
- var _doGridDetect = function (tab) {
- chrome.tabs.sendMessage(tab.id, {
- type: MSG_TYPE.BROWSER_CLICKED,
- event: MSG_TYPE.GRID_DETECT
- });
- };
- /**
- * 查看页面wpo信息
- */
- var _showPageWpoInfo = function (wpoInfo) {
- chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
- if (!wpoInfo) {
- notifyText({
- message: "对不起,检测失败"
- });
- } else {
- chrome.tabs.create({
- url: "template/fehelper_wpo.html?" + btoa(encodeURIComponent(JSON.stringify(wpoInfo))),
- active: true
- });
- }
- });
- };
- /**
- * 获取页面wpo信息
- * @return {[type]}
- */
- var _getPageWpoInfo = function () {
- chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
- var tab = tabs[0];
- //显示桌面提醒
- chrome.tabs.sendMessage(tab.id, {
- type: MSG_TYPE.GET_PAGE_WPO_INFO
- });
- });
- };
- /**
- * 执行JS Tracker
- * @private
- */
- var _doJsTracker = function () {
- chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
- var tab = tabs[0];
- chrome.tabs.executeScript(tab.id, {
- code: "void function(t,r,a,c,k){t.tracker_type='bm';t.tracker_uid='fehelper';"
- + "(k=t.TrackerGlobalEvent)?k.f(r):[(k=t[a]('script')).charset='utf-8',"
- + "k.src='http://www.ucren.com/'+c+'/'+c+'.js?'+Math.random(),"
- + "t.documentElement.appendChild(k)]}(document,'TrackerJSLoad','createElement','tracker') ",
- allFrames: false,
- runAt: 'document_end'
- });
- });
- };
- /**
- * 代码压缩工具
- * @private
- */
- var _goCompressTool = function () {
- var url = "http://www.baidufe.com/fehelper/codecompress.html";
- chrome.tabs.query({windowId: chrome.windows.WINDOW_ID_CURRENT}, function (tabs) {
- var isOpened = false;
- var tabId;
- var reg = new RegExp("fehelper.*codecompress.html$", "i");
- for (var i = 0, len = tabs.length; i < len; i++) {
- if (reg.test(tabs[i].url)) {
- isOpened = true;
- tabId = tabs[i].id;
- break;
- }
- }
- if (!isOpened) {
- chrome.tabs.create({
- url: url,
- active: true
- });
- } else {
- chrome.tabs.update(tabId, {highlighted: true});
- }
- });
- };
- /**
- * 创建或更新成功执行的动作
- * @param evt
- * @param content
- * @private
- */
- var _tabUpdatedCallback = function (evt, content) {
- return function (newTab) {
- if (content) {
- chrome.tabs.sendMessage(newTab.id, {
- type: MSG_TYPE.TAB_CREATED_OR_UPDATED,
- content: content,
- event: evt
- });
- }
- };
- };
- /**
- * 打开对应文件,运行该Helper
- * @param tab
- * @param file
- * @param txt
- * @private
- */
- var _openFileAndRun = function (tab, file, txt) {
- chrome.tabs.query({windowId: chrome.windows.WINDOW_ID_CURRENT}, function (tabs) {
- var isOpened = false;
- var tabId;
- var reg = new RegExp("^chrome.*" + file + ".html$", "i");
- for (var i = 0, len = tabs.length; i < len; i++) {
- if (reg.test(tabs[i].url)) {
- isOpened = true;
- tabId = tabs[i].id;
- break;
- }
- }
- if (!isOpened) {
- chrome.tabs.create({
- url: 'template/fehelper_' + file + '.html',
- active: true
- }, _tabUpdatedCallback(file, txt));
- } else {
- chrome.tabs.update(tabId, {highlighted: true}, _tabUpdatedCallback(file, txt));
- }
- });
- };
- /**
- * 根据给定参数,运行对应的Helper
- */
- var _runHelper = function (config) {
- chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
- var tab = tabs[0];
- // 如果是采用独立文件方式访问,直接打开该页面即可
- if (config.useFile == '1') {
- _openFileAndRun(tab, config.msgType);
- } else {
- switch (config.msgType) {
- //fcphelper检测
- case MSG_TYPE.FCP_HELPER_DETECT:
- _doFcpDetect(tab);
- break;
- //栅格检测
- case MSG_TYPE.GRID_DETECT:
- _doGridDetect(tab);
- break;
- //查看网页加载时间
- case MSG_TYPE.SHOW_PAGE_LOAD_TIME:
- _getPageWpoInfo();
- break;
- //js tracker
- case MSG_TYPE.JS_TRACKER:
- _doJsTracker();
- break;
- //代码压缩
- case MSG_TYPE.CODE_COMPRESS:
- _goCompressTool();
- break;
- }
- }
- });
- };
- /**
- * 创建扩展专属的右键菜单
- */
- var _createContextMenu = function () {
- _removeContextMenu();
- baidu.contextMenuId = chrome.contextMenus.create({
- title: "FeHelper",
- contexts: ['page', 'selection', 'editable', 'link'],
- documentUrlPatterns: ['http://*/*', 'https://*/*']
- });
- chrome.contextMenus.create({
- title: "JSON格式化",
- contexts: ['page', 'selection', 'editable'],
- parentId: baidu.contextMenuId,
- onclick: function (info, tab) {
- chrome.tabs.executeScript(tab.id, {
- code: '(' + (function () {
- return window.getSelection().toString();
- }).toString() + ')()',
- allFrames: false
- }, function (txt) {
- _openFileAndRun(tab, 'jsonformat', txt);
- });
- }
- });
- chrome.contextMenus.create({
- type: 'separator',
- contexts: ['all'],
- parentId: baidu.contextMenuId
- });
- chrome.contextMenus.create({
- title: "字符串编解码",
- contexts: ['page', 'selection', 'editable'],
- parentId: baidu.contextMenuId,
- onclick: function (info, tab) {
- chrome.tabs.executeScript(tab.id, {
- code: '(' + (function () {
- return window.getSelection().toString();
- }).toString() + ')()',
- allFrames: false
- }, function (txt) {
- _openFileAndRun(tab, 'endecode', txt);
- });
- }
- });
- chrome.contextMenus.create({
- type: 'separator',
- contexts: ['all'],
- parentId: baidu.contextMenuId
- });
- chrome.contextMenus.create({
- title: "生成二维码",
- contexts: ['page', 'selection', 'editable', 'link'],
- parentId: baidu.contextMenuId,
- onclick: function (info, tab) {
- chrome.tabs.executeScript(tab.id, {
- code: '(' + (function () {
- return window.getSelection().toString() || location.href;
- }).toString() + ')()',
- allFrames: false
- }, function (txt) {
- _openFileAndRun(tab, 'qrcode', txt);
- });
- }
- });
- chrome.contextMenus.create({
- type: 'separator',
- contexts: ['all'],
- parentId: baidu.contextMenuId
- });
- chrome.contextMenus.create({
- title: "代码格式化",
- contexts: ['page', 'selection', 'editable'],
- parentId: baidu.contextMenuId,
- onclick: function (info, tab) {
- chrome.tabs.executeScript(tab.id, {
- code: '(' + (function () {
- return window.getSelection().toString();
- }).toString() + ')()',
- allFrames: false
- }, function (txt) {
- _openFileAndRun(tab, 'codebeautify', txt);
- });
- }
- });
- chrome.contextMenus.create({
- type: 'separator',
- contexts: ['all'],
- parentId: baidu.contextMenuId
- });
- chrome.contextMenus.create({
- title: "代码压缩",
- contexts: ['page', 'selection', 'editable'],
- parentId: baidu.contextMenuId,
- onclick: function (info, tab) {
- _goCompressTool();
- }
- });
- chrome.contextMenus.create({
- type: 'separator',
- contexts: ['all'],
- parentId: baidu.contextMenuId
- });
- chrome.contextMenus.create({
- title: "页面取色器",
- contexts: ['page', 'selection', 'editable'],
- parentId: baidu.contextMenuId,
- onclick: function (info, tab) {
- _showColorPicker();
- }
- });
- };
- /**
- * 移除扩展专属的右键菜单
- */
- var _removeContextMenu = function () {
- if (!baidu.contextMenuId) return;
- chrome.contextMenus.remove(baidu.contextMenuId);
- baidu.contextMenuId = null;
- };
- /**
- * 创建或移除扩展专属的右键菜单
- */
- var _createOrRemoveContextMenu = function () {
- //管理右键菜单
- if (baidu.feOption.getOptionItem('opt_item_contextMenus') !== 'false') {
- _createContextMenu();
- } else {
- _removeContextMenu();
- }
- };
- /**
- * 显示color picker
- * @private
- */
- var _showColorPicker = function () {
- chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
- var tab = tabs[0];
- var tabid = tab.id;
- var port = chrome.tabs.connect(tabid, {name: "popupshown"});
- chrome.tabs.sendMessage(tabid, {enableColorPicker: true}, function (response) {
- chrome.tabs.sendMessage(tabid, {doPick: true}, function (r) {
- });
- });
- });
- };
- /**
- * 将网页截成一张图,实现取色
- * @param callback
- * @private
- */
- var _drawColorPicker = function (callback) {
- chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
- var tab = tabs[0];
- var tabid = tab.id;
- chrome.tabs.captureVisibleTab(null, {format: 'png'}, function (dataUrl) {
- chrome.tabs.sendMessage(tabid, {
- setPickerImage: true,
- pickerImage: dataUrl
- }, function (response) {
- callback && callback();
- });
- });
- });
- };
- /**
- * 接收来自content_scripts发来的消息
- */
- var _addExtensionListener = function () {
- chrome.runtime.onMessage.addListener(function (request, sender, callback) {
- //处理CSS的请求
- if (request.type == MSG_TYPE.GET_CSS) {
- //直接AJAX获取CSS文件内容
- baidu.network.readFileContent(request.link, callback);
- }
- //处理JS的请求
- else if (request.type == MSG_TYPE.GET_JS) {
- //直接AJAX获取JS文件内容
- baidu.network.readFileContent(request.link, callback);
- }
- //处理HTML的请求
- else if (request.type == MSG_TYPE.GET_HTML) {
- //直接AJAX获取JS文件内容
- baidu.network.readFileContent(request.link, callback);
- }
- //处理cookie
- else if (request.type == MSG_TYPE.GET_COOKIE) {
- baidu.network.getCookies(request, callback);
- }
- //移除cookie
- else if (request.type == MSG_TYPE.REMOVE_COOKIE) {
- baidu.network.removeCookie(request, callback);
- }
- //设置cookie
- else if (request.type == MSG_TYPE.SET_COOKIE) {
- baidu.network.setCookie(request, callback);
- }
- //CSS准备就绪
- else if (request.type == MSG_TYPE.CSS_READY) {
- _readyState.css = true;
- }
- //JS准备就绪
- else if (request.type == MSG_TYPE.JS_READY) {
- _readyState.js = true;
- }
- //HTML准备就绪
- else if (request.type == MSG_TYPE.HTML_READY) {
- _readyState.html = true;
- }
- //提取配置项
- else if (request.type == MSG_TYPE.GET_OPTIONS) {
- baidu.feOption.doGetOptions(request.items, callback);
- }
- //保存配置项
- else if (request.type == MSG_TYPE.SET_OPTIONS) {
- baidu.feOption.doSetOptions(request.items, callback);
- //管理右键菜单
- _createOrRemoveContextMenu();
- }
- //保存当前网页加载时间
- else if (request.type == MSG_TYPE.CALC_PAGE_LOAD_TIME) {
- _showPageWpoInfo(request.wpo);
- }
- // 从popup中点过来的
- else if (request.type == MSG_TYPE.FROM_POPUP) {
- _runHelper(request.config);
- }
- // color picker
- else if (request.type == "color-picker:newImage") {
- _drawColorPicker(callback);
- }
- return true;
- });
- };
- /**
- * 初始化
- */
- var _init = function () {
- _addExtensionListener();
- _detectReadyState();
- _createOrRemoveContextMenu();
- };
- return {
- init: _init,
- runHelper: _runHelper,
- showColorPicker: _showColorPicker
- };
- })();
- //初始化
- BgPageInstance.init();
|