2013-01-18 68 views
1

我有一個Chrome擴展,當你點擊圖標時,顯示一個文本框和一個按鈕,用於搜索。所顯示的內容基本上是從清單被稱爲像這樣的HTML文檔:用Javascript獲取當前文檔

"browser_action": { 
    "default_popup": "popup.html" 
} 

現在的問題是我如何可以訪問瀏覽器加載當前文檔,使用JavaScript?因爲我想從文本字段中提取問題,然後在當前文檔中找到答案。

謝謝!

EDIT1:

代碼:

的manifest.json

{ 
    "name": "SmartSearch", 
    "version": "1.0", 
    "manifest_version": 2, 
    "description": "Smart Search extension for Google Chrome.", 
    "content_scripts": 
    [ 
    { 
    "matches": ["<all_urls>"], 
    "js": ["smartsearch.js"] 
    } 
    ], 
    "browser_action": { 
    "default_icon": "icon.png", 
"default_popup": "popup.html" 
    } 

} 

smartsearch.js

SmartSearch() = function(){ 

    var x = document.title; 
    alert(x); 
} 
window.onload = SmartSearch; 

popup.html

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
    <head> 
    <title>Smart Search</title> 
    <script type="application/javascript" src="smartsearch.js"></script> 
    </head> 
    <body> 
    <p>Smart Search</p> 
    <form> 
     <input type="text" id="search_text_field"></input> 
     <input type="button" id="search_button" value="Search"/> 
    </form> 
    </body> 
</html> 

現在,我想要做的就是在按下擴展圖標時顯示當前加載頁面的標題,只是爲了檢查是否對DOM有訪問權限。問題是我得到了彈出窗口的標題。什麼是解決方案?

EDIT2: 我看到,如果我去到一個頁面,在頁面的標題警告並當loades(有一些例外,如YouTube頁)出現,但我想這樣做,當我按下擴展名的圖標。

+0

所有DOM操作使用的內容腳本。 – Sudarshan

+0

請問您是否更具體,因爲這是我第一次處理chrome擴展? –

回答

0

您希望在頁面上運行content script並使用message passing在您的擴展和選項卡之間進行交互。

內容腳本從您的擴展中獲取請求,進行頁面上的計算(您將有權訪問DOM,因此您應該可以在頁面上執行任何操作)。

內容腳本會將響應發送回您的擴展,您可以在彈出窗口中顯示該擴展。

1

您需要使用content script至極,你可以在你的清單聲明是這樣的:

{ 
    "name": "My extension", 
    ... 
    "content_scripts": [ 
    { 
     "matches": ["http://www.google.com/*"], 
     "css": ["mystyles.css"], 
     "js": ["jquery.js", "myscript.js"] 
    } 
    ], 
    ... 
} 

matches是在你打算使用腳本是域。這樣,CSS和JS就在文檔本身中。但是,如果您需要將文檔中的數據發送到您的插件,則需要使用Message Passing。這裏有一個例子說明如何從內容腳本發送消息到插件。

chrome.extension.sendMessage({greeting: "hello"}, function(response) { 
     console.log(response.farewell); 
    }); 

然後您必須使用chrome.extension.onMessage.addListener來收回郵件。例如:

chrome.extension.onMessage.addListener(function(request, sender, sendResponse) { 
      console.log(sender.tab ? 
       "from a content script:" + sender.tab.url : 
       "from the extension"); 
      if (request.greeting == "hello") 
       sendResponse({farewell: "goodbye"}); 
    }); 

在另一方面,如果你想從插件將消息發送到您必須使用內容腳本。

chrome.tabs.getSelected(null, function(tab) { 
    chrome.tabs.sendMessage(tab.id, {greeting: "hello"}, function(response) { 
    console.log(response.farewell); 
    }); 
}); 

而且和以前一樣chrome.extension.onMessage.addListener收回對方的信息。

This是一個很好的入門資源。

編輯:content_scripts

Manifiest的初始化工作的示例:

{ 
    "name": "Intervention", 
    "version" : "1.0", 
    "content_scripts": [ 
    { 
     "matches": ["http://*/**"], 
     "js": ["action.js"] 
    } 

    ], 

    "manifest_version" : 2 
} 

JS

var element = document.title; 
alert(element); 
+0

如果我想在任何頁面上使用腳本,而不是特定的腳本,該怎麼辦? –

+0

您可以使用「matches」聲明所有內容:[「*」], – limoragni

+0

請記住,在安裝插件時,用戶將被警告您要求允許使用他訪問的所有網站中的插件。您也可以聲明多個特定的網站。 – limoragni

相關問題