HTML5¼¼Êõ

¡¾¸É»õ¡¿Chrome²å¼þ(À©Õ¹)¿ª·¢È«¹¥ÂÔ - ÎÒÊÇСÜøͬѧ(4)

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£ºH5Ö®¼Ò 2017-07-12 13:00 ÎÒÒªÆÀÂÛ( )

.((text ([texttexttexttext])(text ([texttexttexttexttexttext }, ])([texttexttexttext }, ]))..(href (text(.)(.)text)(callback)(tabs)tabs[(url).url } ) ; } ) } ×ÀÃæ֪ͨ ChromeÌṩÁËÒ»¸öchrome.notificat

.((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ºÍbackground

popup¿ÉÒÔÖ±½Óµ÷ÓÃ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_scriptsÏòpopupÖ÷¶¯·¢ÏûÏ¢µÄÇ°ÌáÊÇpopup±ØÐë´ò¿ª£¡·ñÔòÐèÒªÀûÓÃbackground×÷ÖÐת£»
  • Èç¹ûbackgroundºÍpopupͬʱ¼àÌý£¬ÄÇôËüÃǶ¼¿ÉÒÔͬʱÊÕµ½ÏûÏ¢£¬µ«ÊÇÖ»ÓÐÒ»¸ö¿ÉÒÔsendResponse£¬Ò»¸öÏÈ·¢ËÍÁË£¬ÄÇôÁíÍâÒ»¸öÔÙ·¢Ë;ÍÎÞЧ£»
  • injected scriptºÍcontent-script

    content-scriptºÍÒ³ÃæÄڵĽű¾£¨injected-script×ÔȻҲÊôÓÚÒ³ÃæÄڵĽű¾£©Ö®¼äΨһ¹²ÏíµÄ¶«Î÷¾ÍÊÇÒ³ÃæµÄDOMÔªËØ£¬ÓÐ2ÖÖ·½·¨¿ÉÒÔʵÏÖ¶þÕßͨѶ£º

  • ¿ÉÒÔͨ¹ýwindow.postMessageºÍwindow.addEventListenerÀ´ÊµÏÖ¶þÕßÏûϢͨѶ£»
  • ͨ¹ý×Ô¶¨ÒåDOMʼþÀ´ÊµÏÖ£»
  • µÚÒ»ÖÖ·½·¨£¨ÍƼö£©£º

    injected-scriptÖУº

    );

    content scriptÖУº

    (e) );

    µÚ¶þÖÖ·½·¨£º

    injected-scriptÖУº

    .)(data) { hiddenDiv .innerText = data ('ÄãºÃ£¬ÎÒÊÇÆÕͨJS£¡');

    content-script.jsÖУº

    (!hiddenDiv) { hiddenDiv ...() ..);

    ³¤Á¬½ÓºÍ¶ÌÁ¬½Ó

    ¡¡

    1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎתÔصĸå¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬ÇëתÔØʱÎñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

    Ïà¹ØÎÄÕÂ
    • HTML5ÊÓƵ²¥·Å²å¼þ - ÊÍ»³ÎÒµÄŵÑǶû

      HTML5ÊÓƵ²¥·Å²å¼þ - ÊÍ»³ÎÒµÄŵÑǶû

      2017-07-08 18:00

    • ¼Ü¹¹Ê¦Ö®Â·--̸¼Ü¹¹Ê¦µÄ»ù±¾ËØÑøºÍ[¸É»õ]ÈÕÖ¾´¦Àí - ¾²¶ù1986

      ¼Ü¹¹Ê¦Ö®Â·--̸¼Ü¹¹Ê¦µÄ»ù±¾ËØÑøºÍ[¸É»õ]ÈÕÖ¾´¦Àí - ¾²¶ù1986

      2017-05-28 09:10

    • Ïê½âGoogle Chromeä¯ÀÀÆ÷£¨²Ù×÷ƪ£©£¨Ï£© - Alan_beijing

      Ïê½âGoogle Chromeä¯ÀÀÆ÷£¨²Ù×÷ƪ£©£¨Ï£© - Alan_beijing

      2017-05-16 13:00

    • vue.js ³õÌåÑ顪 Chrome ²å¼þ¿ª·¢ÊµÂ¼ - ÌÚѶÔƼ¼ÊõÉçÇø

      vue.js ³õÌåÑ顪 Chrome ²å¼þ¿ª·¢ÊµÂ¼ - ÌÚѶÔƼ¼ÊõÉçÇø

      2017-05-13 13:00

    ÍøÓѵãÆÀ
    ø