<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                參考博客[https://www.cnblogs.com/liuxianan/p/chrome-plugin-develop.html](https://www.cnblogs.com/liuxianan/p/chrome-plugin-develop.html) ## **權限對比** | JS種類 | 可訪問的API | DOM訪問情況 | JS訪問情況 | 直接跨域 |調試方式| | --- | --- | --- | --- | --- |--- | | injected script | 和普通JS無任何差別,不能訪問任何擴展API | 可以訪問 | 可以訪問 | 不可以 |F12| | content script | 只能訪問 extension、runtime等部分API | 可以訪問 | 不可以 | 不可以 |F12的Console中的Top選項![](images/screenshot_1576392961270.png)| | popup js | 可訪問絕大部分API,除了devtools系列 | 不可直接訪問,但是可以通過chrome.tabs.executeScript來執行腳本 | 不可以 | 可以 |選中插件圖標右鍵審查彈出內容| | background js | 可訪問絕大部分API,除了devtools系列 | 不可直接訪問,但是可以通過chrome.tabs.executeScript來執行腳本 | 不可以 | 可以 |[chrome://extensions/](chrome://extensions/)中每個插件的背景頁| | devtools js | 只能訪問 devtools、extension、runtime等部分API | 可以 | 可以 | 不可以 |F12選中插件選項右鍵審查內容| ## **[消息通信](https://developer.chrome.com/extensions/messaging)** 注:`-`表示不存在或者無意義,或者待驗證。 | | 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: ~~~ // background.js function test() { alert('我是background!'); } // popup.js var bg = chrome.extension.getBackgroundPage(); bg.test(); // 訪問bg的函數 alert(bg.document.body.innerHTML); // 訪問bg的DOM ~~~ > 小插曲,今天碰到一個情況,發現popup無法獲取background的任何方法,找了半天才發現是因為background的js報錯了,而你如果不主動查看background的js的話,是看不到錯誤信息的,特此提醒。 至于`background`訪問`popup`如下(前提是`popup`已經打開): ~~~ var views = chrome.extension.getViews({type:'popup'}); if(views.length > 0) { console.log(views[0].location.href); } ~~~ ### popup或者bg向content主動發送消息 background.js或者popup.js: ~~~ function sendMessageToContentScript(message, callback) { chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { chrome.tabs.sendMessage(tabs[0].id, message, function(response) { if(callback) callback(response); }); }); } sendMessageToContentScript({cmd:'test', value:'你好,我是popup!'}, function(response) { console.log('來自content的回復:'+response); }); ~~~ `content-script.js`接收: ~~~ chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { // console.log(sender.tab ?"from a content script:" + sender.tab.url :"from the extension"); if(request.cmd == 'test') alert(request.value); sendResponse('我收到了你的消息!'); }); ~~~ 雙方通信直接發送的都是JSON對象,不是JSON字符串,所以無需解析,很方便(當然也可以直接發送字符串)。 > 網上有些老代碼中用的是`chrome.extension.onMessage`,沒有完全查清二者的區別(貌似是別名),但是建議統一使用`chrome.runtime.onMessage`。 ### content-script主動發消息給后臺 content-script.js: ~~~ chrome.runtime.sendMessage({greeting: '你好,我是content-script呀,我主動發消息給后臺!'}, function(response) { console.log('收到來自后臺的回復:' + response); }); ~~~ background.js 或者 popup.js: ~~~ // 監聽來自content-script的消息 chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { console.log('收到來自content-script的消息:'); console.log(request, sender, sendResponse); sendResponse('我是后臺,我已收到你的消息:' + JSON.stringify(request)); }); ~~~ 注意事項: * content\_scripts向`popup`主動發消息的前提是popup必須打開!否則需要利用background作中轉; * 如果background和popup同時監聽,那么它們都可以同時收到消息,但是只有一個可以sendResponse,一個先發送了,那么另外一個再發送就無效; ### injected script和content-script `content-script`和頁面內的腳本(`injected-script`自然也屬于頁面內的腳本)之間唯一共享的東西就是頁面的DOM元素,有2種方法可以實現二者通訊: 1. 可以通過`window.postMessage`和`window.addEventListener`來實現二者消息通訊; 2. 通過自定義DOM事件來實現; 第一種方法(推薦): `injected-script`中: ~~~ window.postMessage({"test": '你好!'}, '*'); ~~~ content script中: ~~~ window.addEventListener("message", function(e) { console.log(e.data); }, false); ~~~ 第二種方法: `injected-script`中: ~~~ var customEvent = document.createEvent('Event'); customEvent.initEvent('myCustomEvent', true, true); function fireCustomEvent(data) { hiddenDiv = document.getElementById('myCustomEventDiv'); hiddenDiv.innerText = data hiddenDiv.dispatchEvent(customEvent); } fireCustomEvent('你好,我是普通JS!'); ~~~ `content-script.js`中: ~~~ var hiddenDiv = document.getElementById('myCustomEventDiv'); if(!hiddenDiv) { hiddenDiv = document.createElement('div'); hiddenDiv.style.display = 'none'; document.body.appendChild(hiddenDiv); } hiddenDiv.addEventListener('myCustomEvent', function() { var eventData = document.getElementById('myCustomEventDiv').innerText; console.log('收到自定義事件消息:' + eventData); }); ~~~ ## 長連接和短連接 其實上面已經涉及到了,這里再單獨說明一下。Chrome插件中有2種通信方式,一個是短連接(`chrome.tabs.sendMessage`和`chrome.runtime.sendMessage`),一個是長連接(`chrome.tabs.connect`和`chrome.runtime.connect`)。 短連接的話就是擠牙膏一樣,我發送一下,你收到了再回復一下,如果對方不回復,你只能重新發,而長連接類似`WebSocket`會一直建立連接,雙方可以隨時互發消息。 短連接上面已經有代碼示例了,這里只講一下長連接。 popup.js: ~~~ getCurrentTabId((tabId) => { var port = chrome.tabs.connect(tabId, {name: 'test-connect'}); port.postMessage({question: '你是誰啊?'}); port.onMessage.addListener(function(msg) { alert('收到消息:'+msg.answer); if(msg.answer && msg.answer.startsWith('我是')) { port.postMessage({question: '哦,原來是你啊!'}); } }); }); ~~~ content-script.js: ~~~ // 監聽長連接 chrome.runtime.onConnect.addListener(function(port) { console.log(port); if(port.name == 'test-connect') { port.onMessage.addListener(function(msg) { console.log('收到長連接消息:', msg); if(msg.question == '你是誰啊?') port.postMessage({answer: '我是你爸!'}); }); } }); ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看