.((text ([ texttexttexttext])(text ([ texttexttexttexttexttext}, ])([ texttexttexttext}, ]))..(href (text(.)(.)text)(callback) (tabs) tabs[(url) .url}); }) }
×ÀÃæ֪ͨChromeÌṩÁËÒ»¸öchrome.notificationsAPIÒÔ±ã²å¼þÍÆËÍ×ÀÃæ֪ͨ£¬ÔÝδÕÒµ½chrome.notificationsºÍHTML5×Ô´øµÄNotificationµÄÏÔÖøÇø±ð¼°ÓÅÊÆ¡£
ÔÚºǫ́JSÖУ¬ÎÞÂÛÊÇʹÓÃchrome.notifications»¹ÊÇNotification¶¼²»ÐèÒªÉêÇëȨÏÞ£¨HTML5·½Ê½ÐèÒªÉêÇëȨÏÞ£©£¬Ö±½ÓʹÓü´¿É¡£
×î¼òµ¥µÄ֪ͨ£º
´úÂ룺
);
֪ͨµÄÑùʽ¿ÉÒԺܷḻ£º
Õâ¸öûÓÐÉîÈëÑо¿£¬ÓÐÐèÒªµÄ¿ÉÒÔÈ¥¿´¹Ù·½Îĵµ¡£
5ÖÖÀàÐ͵ÄJS¶Ô±ÈChrome²å¼þµÄJSÖ÷Òª¿ÉÒÔ·ÖΪÕâ5Àࣺinjected script¡¢content-script¡¢popup js¡¢background jsºÍdevtools js£¬
ȨÏÞ¶Ô±ÈJSÖÖÀà ¿É·ÃÎʵÄAPI DOM·ÃÎÊÇé¿ö JS·ÃÎÊÇé¿ö Ö±½Ó¿çÓò
injected script ºÍÆÕͨJSÎÞÈκβî±ð£¬²»ÄÜ·ÃÎÊÈκÎÀ©Õ¹API ¿ÉÒÔ·ÃÎÊ ¿ÉÒÔ·ÃÎÊ ²»¿ÉÒÔ
content script Ö»ÄÜ·ÃÎÊ extension¡¢runtimeµÈ²¿·ÖAPI ¿ÉÒÔ·ÃÎÊ ²»¿ÉÒÔ ²»¿ÉÒÔ
popup js ¿É·ÃÎʾø´ó²¿·ÖAPI£¬³ýÁËdevtoolsϵÁÐ ²»¿ÉÖ±½Ó·ÃÎÊ ²»¿ÉÒÔ ¿ÉÒÔ
background js ¿É·ÃÎʾø´ó²¿·ÖAPI£¬³ýÁËdevtoolsϵÁÐ ²»¿ÉÖ±½Ó·ÃÎÊ ²»¿ÉÒÔ ¿ÉÒÔ
devtools js Ö»ÄÜ·ÃÎÊ devtools¡¢extension¡¢runtimeµÈ²¿·ÖAPI ¿ÉÒÔ ¿ÉÒÔ ²»¿ÉÒÔ
µ÷ÊÔ·½Ê½¶Ô±ÈJSÀàÐÍ µ÷ÊÔ·½Ê½
injected script Ö±½ÓÆÕͨµÄF12¼´¿É
content-script ´ò¿ªConsole,ÈçͼÇл»
popup-js popupÒ³ÃæÓÒ¼üÉó²éÔªËØ
background ²å¼þ¹ÜÀíÒ³µã»÷±³¾°Ò³¼´¿É
devtools-js ÔÝδÕÒµ½ÓÐЧ·½·¨
ÏûϢͨÐÅͨÐÅÖ÷Ò³£ºhttps://developer.chrome.com/extensions/messaging
Ç°ÃæÎÒÃǽéÉÜÁËChrome²å¼þÖдæÔÚµÄ5ÖÖJS£¬ÄÇôËüÃÇÖ®¼äÈçºÎ»¥ÏàͨÐÅÄØ£¿ÏÂÃæÏÈÀ´ÏµÍ³¸Å¿öһϣ¬È»ºóÔÙ·ÖÀàϸ˵¡£ÐèÒªÖªµÀµÄÊÇ£¬popupºÍbackgroundÆäʵ¼¸ºõ¿ÉÒÔÊÓΪһÖÖ¶«Î÷£¬ÒòΪËüÃÇ¿É·ÃÎʵÄAPI¶¼Ò»Ñù¡¢Í¨ÐÅ»úÖÆÒ»Ñù¡¢¶¼¿ÉÒÔ¿çÓò¡£
»¥ÏàͨПÅÀÀ×¢£º-±íʾ²»´æÔÚ»òÕßÎÞÒâÒ壬»òÕß´ýÑéÖ¤¡£
injected-script content-script popup-js background-js
injected-script - window.postMessage - -
content-script window.postMessage - chrome.runtime.sendMessage chrome.runtime.connect chrome.runtime.sendMessage chrome.runtime.connect
popup-js - chrome.tabs.sendMessage chrome.tabs.connect - chrome.extension. getBackgroundPage()
background-js - chrome.tabs.sendMessage chrome.tabs.connect chrome.extension.getViews -
devtools-js chrome.devtools. inspectedWindow.eval - chrome.runtime.sendMessage chrome.runtime.sendMessage
ͨÐÅÏêϸ½éÉÜ popupºÍbackgroundpopup¿ÉÒÔÖ±½Óµ÷ÓÃbackgroundÖеÄJS·½·¨£¬Ò²¿ÉÒÔÖ±½Ó·ÃÎÊbackgroundµÄDOM£º
() bg .(bg.document.body.innerHTML); // ·ÃÎÊbgµÄDOM
С²åÇú£¬½ñÌìÅöµ½Ò»¸öÇé¿ö£¬·¢ÏÖpopupÎÞ·¨»ñÈ¡backgroundµÄÈκη½·¨£¬ÕÒÁË°ëÌì²Å·¢ÏÖÊÇÒòΪbackgroundµÄjs±¨´íÁË£¬¶øÄãÈç¹û²»Ö÷¶¯²é¿´backgroundµÄjsµÄ»°£¬ÊÇ¿´²»µ½´íÎóÐÅÏ¢µÄ£¬ÌØ´ËÌáÐÑ¡£
ÖÁÓÚbackground·ÃÎÊpopupÈçÏ£¨Ç°ÌáÊÇpopupÒѾ´ò¿ª£©£º
))
popup»òÕßbgÏòcontentÖ÷¶¯·¢ËÍÏûÏ¢background.js»òÕßpopup.js£º
function sendMessageToContentScript(message, callback) (tabs) message, function(response) )((response) response); });
content-script.js½ÓÊÕ£º
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) () );
Ë«·½Í¨ÐÅÖ±½Ó·¢Ë͵Ķ¼ÊÇJSON¶ÔÏ󣬲»ÊÇJSON×Ö·û´®£¬ËùÒÔÎÞÐè½âÎö£¬ºÜ·½±ã£¨µ±È»Ò²¿ÉÒÔÖ±½Ó·¢ËÍ×Ö·û´®£©¡£
ÍøÉÏÓÐЩÀÏ´úÂëÖÐÓõÄÊÇchrome.extension.onMessage£¬Ã»ÓÐÍêÈ«²éÇå¶þÕßµÄÇø±ð(òËÆÊDZðÃû)£¬µ«Êǽ¨ÒéͳһʹÓÃchrome.runtime.onMessage¡£
content-scriptÖ÷¶¯·¢ÏûÏ¢¸øºǫ́content-script.js£º
(response) response); });
background.js »òÕß popup.js£º
// ¼àÌýÀ´×Ôcontent-scriptµÄÏûÏ¢ chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) ..stringify(request)); });
×¢ÒâÊÂÏ
content-scriptºÍÒ³ÃæÄڵĽű¾£¨injected-script×ÔȻҲÊôÓÚÒ³ÃæÄڵĽű¾£©Ö®¼äΨһ¹²ÏíµÄ¶«Î÷¾ÍÊÇÒ³ÃæµÄDOMÔªËØ£¬ÓÐ2ÖÖ·½·¨¿ÉÒÔʵÏÖ¶þÕßͨѶ£º
µÚÒ»ÖÖ·½·¨£¨ÍƼö£©£º
injected-scriptÖУº
);
content scriptÖУº
(e) );
µÚ¶þÖÖ·½·¨£º
injected-scriptÖУº
.)(data) { hiddenDiv .innerText = data ('ÄãºÃ£¬ÎÒÊÇÆÕͨJS£¡');
content-script.jsÖУº
(!hiddenDiv) { hiddenDiv ...() ..);
³¤Á¬½ÓºÍ¶ÌÁ¬½Ó
¡¡