0

你好,我想加載腳本,無論用戶是否點擊我的擴展圖標 這是我的擴展,它工作的很好,但我希望它沒有讓用戶點擊該圖標加載腳本..Chrome擴展加載腳本,無需點擊圖標

這是代碼。

{ 
    "name": "Injecta", 
    "version": "0.0.1", 
    "manifest_version": 2, 
     "description": "Injecting stuff", 

     "background": 
     { 
     "scripts": ["jquery.js","background.js"] 
       }, 
      "browser_action": { 
      "default_title": "Inject!" 
       }, 
       "permissions": [ 
       "https://*/*", 
       "http://*/*", 
       "tabs" 
        ] 
        } 

這是我background.js

 chrome.browserAction.onClicked.addListener(function (tab) { 
     chrome.tabs.executeScript({ 
     file: 'jquery.js' 
      }); 
     chrome.tabs.executeScript({ 
      file: 'inject.js' 
      }); 
       }); 

我只是想延長加載所有頁面加載的腳本。目前用戶必須點擊圖標來加載腳本..

+0

http://stackoverflow.com/questions/33484726/jquery-is-not-defined-in-chromes-javascript-console – miqdadamirali

+0

沒有公認的答案.. – Lisa

回答

1

什麼executeScript確實基本上是創建一個動態Content Script。這被稱爲Programmatic Injection

使用內容腳本的替代方法是specifying them in the manifest。這實現了你所要求的內容:內容腳本在頁面加載時自動執行。

"content_scripts" : [ 
    { 
    "js": ["jquery.js", "inject.js"], 
    "matches": ["*://*/*"] 
    } 
], 

調整matches參數,只包括match patterns你希望它運行在頁面上。

如果您需要在注射發生時進行微調,請務必查看run_at參數的文檔。

+0

你能也幫我把數據附加到當前頁面的div上。 – Lisa

+0

可能,但這應該是一個新問題。它也似乎是一個非常基本的問題 - 確保你[先做你的研究](http://stackoverflow.com/help/how-to-ask)。 – Xan

-2

if (typeof jQuery === 'undefined') {}

+0

這將不添加jQuery的.. – Lisa

相關問題