fe-background.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /**
  2. * FE-Helper后台运行程序
  3. * @author [email protected]
  4. */
  5. var BgPageInstance = (function(){
  6. //各种元素的就绪情况
  7. var _readyState = {
  8. css : false,
  9. js : false,
  10. html : true,
  11. allDone : false
  12. };
  13. //侦测的interval
  14. var _detectInterval = null;
  15. //侦测就绪情况
  16. var _detectReadyState = function(){
  17. _detectInterval = window.setInterval(function(){
  18. if(_readyState.css && _readyState.js && _readyState.html) {
  19. _readyState.allDone = true;
  20. window.clearInterval(_detectInterval);
  21. }
  22. },100);
  23. };
  24. /**
  25. * 执行前端FCPHelper检测
  26. */
  27. var _doFcpDetect = function(tab){
  28. //所有元素都准备就绪
  29. if(_readyState.allDone) {
  30. chrome.tabs.sendMessage(tab.id, {
  31. type: MSG_TYPE.BROWSER_CLICKED,
  32. event : MSG_TYPE.FCP_HELPER_DETECT
  33. });
  34. } else {
  35. //正在准备数据,请稍等...
  36. //显示桌面提醒
  37. baidu.feNotification.notifyText({
  38. message : "正在准备数据,请稍等..."
  39. });
  40. }
  41. };
  42. /**
  43. * 执行栅格检测
  44. */
  45. var _doGridDetect = function(tab){
  46. chrome.tabs.sendMessage(tab.id, {
  47. type: MSG_TYPE.BROWSER_CLICKED,
  48. event : MSG_TYPE.GRID_DETECT
  49. });
  50. };
  51. var notId=0;
  52. /**
  53. * 提醒层 缓存
  54. * @type {Array}
  55. */
  56. var _notificationCache = [];
  57. /**
  58. * 查看页面wpo信息
  59. */
  60. var _showPageWpoInfo = function(wpoInfo){
  61. chrome.tabs.getSelected(null,function(tab){
  62. try{
  63. _notificationCache[tab.id].cancel();
  64. }catch (e){}
  65. if(!wpoInfo) {
  66. baidu.feNotification.notifyText({
  67. message : "对不起,检测失败"
  68. });
  69. }else{
  70. if(window.webkitNotifications && webkitNotifications.createHTMLNotification) {
  71. baidu.feNotification.notifyHtml("template/fehelper_wpo.html?" + JSON.stringify(wpoInfo));
  72. } else {
  73. chrome.tabs.create({
  74. url : "template/fehelper_wpo.html?" + JSON.stringify(wpoInfo),
  75. selected : true
  76. });
  77. }
  78. }
  79. });
  80. };
  81. /**
  82. * 获取页面wpo信息
  83. * @return {[type]}
  84. */
  85. var _getPageWpoInfo = function(){
  86. chrome.tabs.getSelected(null,function(tab){
  87. //显示桌面提醒
  88. _notificationCache[tab.id] = baidu.feNotification.notifyText({
  89. message : "正在统计,请稍后...",
  90. autoClose : false
  91. });
  92. chrome.tabs.sendMessage(tab.id,{
  93. type : MSG_TYPE.GET_PAGE_WPO_INFO
  94. });
  95. });
  96. };
  97. /**
  98. * 执行JS Tracker
  99. * @private
  100. */
  101. var _doJsTracker = function(){
  102. chrome.tabs.getSelected(null,function(tab){
  103. chrome.tabs.executeScript(tab.id,{
  104. code : "void function(t,r,a,c,k){t.tracker_type='bm';t.tracker_uid='fehelper';"
  105. + "(k=t.TrackerGlobalEvent)?k.f(r):[(k=t[a]('script')).charset='utf-8',"
  106. + "k.src='http://www.ucren.com/'+c+'/'+c+'.js?'+Math.random(),"
  107. + "t.documentElement.appendChild(k)]}(document,'TrackerJSLoad','createElement','tracker') ",
  108. allFrames : false,
  109. runAt : 'document_end'
  110. });
  111. });
  112. };
  113. /**
  114. * 代码压缩工具
  115. * @private
  116. */
  117. var _goCompressTool = function(){
  118. var url = "http://www.baidufe.com/fehelper/codecompress.html";
  119. chrome.tabs.getAllInWindow(null,function(tabs){
  120. var isOpened = false;
  121. var tabId ;
  122. var reg = new RegExp("fehelper.*codecompress.html$","i");
  123. for(var i = 0,len = tabs.length;i < len;i++){
  124. if(reg.test(tabs[i].url)) {
  125. isOpened = true;
  126. tabId = tabs[i].id;
  127. break;
  128. }
  129. }
  130. if(!isOpened) {
  131. chrome.tabs.create({
  132. url : url,
  133. selected : true
  134. });
  135. } else {
  136. chrome.tabs.update(tabId,{selected : true});
  137. }
  138. });
  139. };
  140. /**
  141. * 打开对应文件,运行该Helper
  142. * @param tab
  143. * @param file
  144. * @private
  145. */
  146. var _openFileAndRun = function(tab,file){
  147. chrome.tabs.getAllInWindow(null,function(tabs){
  148. var isOpened = false;
  149. var tabId ;
  150. var reg = new RegExp("^chrome.*" + file + ".html$","i");
  151. for(var i = 0,len = tabs.length;i < len;i++){
  152. if(reg.test(tabs[i].url)) {
  153. isOpened = true;
  154. tabId = tabs[i].id;
  155. break;
  156. }
  157. }
  158. if(!isOpened) {
  159. chrome.tabs.create({
  160. url : 'template/fehelper_' + file + '.html',
  161. selected : true
  162. });
  163. } else {
  164. chrome.tabs.update(tabId,{selected : true});
  165. }
  166. });
  167. };
  168. /**
  169. * 根据给定参数,运行对应的Helper
  170. */
  171. var _runHelper = function(config){
  172. chrome.tabs.getSelected(null,function(tab){
  173. // 如果是采用独立文件方式访问,直接打开该页面即可
  174. if(config.useFile == '1') {
  175. _openFileAndRun(tab,config.msgType);
  176. }else{
  177. switch(config.msgType) {
  178. //fcphelper检测
  179. case MSG_TYPE.FCP_HELPER_DETECT:
  180. _doFcpDetect(tab);
  181. break;
  182. //栅格检测
  183. case MSG_TYPE.GRID_DETECT:
  184. _doGridDetect(tab);
  185. break;
  186. //查看网页加载时间
  187. case MSG_TYPE.SHOW_PAGE_LOAD_TIME:
  188. _getPageWpoInfo();
  189. break;
  190. //js tracker
  191. case MSG_TYPE.JS_TRACKER:
  192. _doJsTracker();
  193. break;
  194. //代码压缩
  195. case MSG_TYPE.CODE_COMPRESS:
  196. _goCompressTool();
  197. break;
  198. }
  199. }
  200. });
  201. };
  202. /**
  203. * 创建扩展专属的右键菜单
  204. */
  205. var _createContextMenu = function(){
  206. _removeContextMenu();
  207. baidu.contextMenuId = chrome.contextMenus.create({
  208. title : "FeHelper-FE助手"
  209. });
  210. chrome.contextMenus.create({
  211. title : "编码检测",
  212. parentId : baidu.contextMenuId,
  213. onclick : function(info,tab) {
  214. //编码检测
  215. _doFcpDetect(tab);
  216. }
  217. });
  218. chrome.contextMenus.create({
  219. title : "栅格检测",
  220. parentId : baidu.contextMenuId,
  221. onclick : function(info,tab) {
  222. //执行栅格检测
  223. _doGridDetect(tab);
  224. }
  225. });
  226. chrome.contextMenus.create({
  227. title : "网页加载耗时",
  228. parentId : baidu.contextMenuId,
  229. onclick : function(info,tab) {
  230. //网页加载耗时
  231. _getPageWpoInfo();
  232. }
  233. });
  234. };
  235. /**
  236. * 移除扩展专属的右键菜单
  237. */
  238. var _removeContextMenu = function(){
  239. if(!baidu.contextMenuId) return;
  240. chrome.contextMenus.remove(baidu.contextMenuId);
  241. baidu.contextMenuId = null;
  242. };
  243. /**
  244. * 创建或移除扩展专属的右键菜单
  245. */
  246. var _createOrRemoveContextMenu = function(){
  247. //管理右键菜单
  248. if(baidu.feOption.getOptionItem('opt_item_contextMenus') === 'true') {
  249. _createContextMenu();
  250. } else {
  251. _removeContextMenu();
  252. }
  253. };
  254. /**
  255. * 接收来自content_scripts发来的消息
  256. */
  257. var _addExtensionListener = function(){
  258. chrome.extension.onMessage.addListener(function(request,sender,callback){
  259. //处理CSS的请求
  260. if(request.type == MSG_TYPE.GET_CSS) {
  261. //直接AJAX获取CSS文件内容
  262. baidu.network.readFileContent(request.link,callback);
  263. }
  264. //处理JS的请求
  265. else if(request.type == MSG_TYPE.GET_JS) {
  266. //直接AJAX获取JS文件内容
  267. baidu.network.readFileContent(request.link,callback);
  268. }
  269. //处理HTML的请求
  270. else if(request.type == MSG_TYPE.GET_HTML) {
  271. //直接AJAX获取JS文件内容
  272. baidu.network.readFileContent(request.link,callback);
  273. }
  274. //处理cookie
  275. else if(request.type == MSG_TYPE.GET_COOKIE) {
  276. baidu.network.getCookies(request,callback);
  277. }
  278. //移除cookie
  279. else if(request.type == MSG_TYPE.REMOVE_COOKIE) {
  280. baidu.network.removeCookie(request,callback);
  281. }
  282. //设置cookie
  283. else if(request.type == MSG_TYPE.SET_COOKIE) {
  284. baidu.network.setCookie(request,callback);
  285. }
  286. //CSS准备就绪
  287. else if(request.type == MSG_TYPE.CSS_READY) {
  288. _readyState.css = true;
  289. }
  290. //JS准备就绪
  291. else if(request.type == MSG_TYPE.JS_READY) {
  292. _readyState.js = true;
  293. }
  294. //HTML准备就绪
  295. else if(request.type == MSG_TYPE.HTML_READY) {
  296. _readyState.html = true;
  297. }
  298. //提取配置项
  299. else if(request.type == MSG_TYPE.GET_OPTIONS){
  300. baidu.feOption.doGetOptions(request.items,callback);
  301. }
  302. //保存配置项
  303. else if(request.type == MSG_TYPE.SET_OPTIONS){
  304. baidu.feOption.doSetOptions(request.items,callback);
  305. //管理右键菜单
  306. _createOrRemoveContextMenu();
  307. }
  308. //保存当前网页加载时间
  309. else if(request.type == MSG_TYPE.CALC_PAGE_LOAD_TIME){
  310. _showPageWpoInfo(request.wpo);
  311. }
  312. return true;
  313. });
  314. };
  315. /**
  316. * 初始化
  317. */
  318. var _init = function(){
  319. _addExtensionListener();
  320. _detectReadyState();
  321. _createOrRemoveContextMenu();
  322. };
  323. return {
  324. init : _init,
  325. runHelper : _runHelper
  326. };
  327. })();
  328. //初始化
  329. BgPageInstance.init();