<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>

                ## background 通常把需要長時間運行的、全局的代碼放在`background`里面,它運行在一個獨立運行環境,其實也可以說是運行在“后臺”的一個頁面,它是與當前瀏覽頁面無關的。 在Manifest配置中,可以通過`page`指定一張網頁,也可以通過`scripts`直接指定一個JS,Chrome會自動為這個JS生成一個默認的網頁。 ``` { "background": { // "page": "background.html", "scripts": ["js/background.js"] } } ``` **1. Google翻譯** 使用Google翻譯選中文本(實例:`Custom Contextmenus`): ``` // manifest.json { "background": { "scripts": ["background.js"] }, // 申請權限 "permissions": [ "tabs", "contextMenus" ] } // background.js chrome.contextMenus.create({ title: 'Google翻譯:%s', contexts: ['selection'], onclick: function(info, tab) { chrome.tabs.create({url: 'https://translate.google.cn/#en/zh-CN/' + encodeURI(info.selectionText)}); } }, function() { console.log(chrome.extension.lastError); }) ``` `%s`可以自動獲取到選中文本。 ![](https://box.kancloud.cn/b8dae8438cd0045fcdb559f1a4b55b5e_370x178.png) `background`調試:打開`chrome://extensions/ `,且激活開發者模式,點擊對應插件“檢查視圖”后的“背景頁” 更多關于右鍵菜單:《瀏覽器的UI界面 - > [右鍵菜單](右鍵菜單.md)》 可以在下面的地方(記得勾選開發者模式)查看`background`運行情況: ![](https://box.kancloud.cn/f0c5c4fcb629eff49133c9b1d5c5e884_597x165.png) **何時使用** 需要持續運行在后臺的 **event-pages** 除了上面的**后臺頁面**`background`,還有一種**事件頁面**`event-pages`,它是在被需要時加載,在空閑時被關閉的后臺腳本。 要使用它,只需簡單的改一下`manifest.json`: ``` { "background": { "scripts": ["event-page.js"], "persistent": false }, } ``` **后臺頁面和事件頁面的區別** 兩者區別在于,后臺頁面(`background`)持續運行,它的生存周期和瀏覽器相同,也就是說從打開瀏覽器到關閉瀏覽器期間,后臺腳本一直在運行,一直占據著內存等系統資源;而事件頁面(`event page`)只在需要活動時活動,在完全不活動的狀態持續幾秒后,chrome將會終止其運行,從而釋放其占據的系統資源,而在再次有事件需要后臺腳本來處理時,重新載入它。 `background`完整API: [https://developer.chrome.com/extensions/background_pages](https://developer.chrome.com/extensions/background_pages)
                  <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>

                              哎呀哎呀视频在线观看