2012-04-16 20 views
1

我對整個Chrome擴展世界非常陌生。Popup中顯示源代碼的特定行

我已經閱讀了教程「hello world」的網頁,並試圖獲得對content_scripts和background.html的理解 - 但我可能已經過量了,似乎無法找到答案,我肯定是一個簡單的任務。

在網站包含以下隱藏的HTML標籤:

<div class="XYZ"> 
<input id="form_ID" type="hidden" value="REF_CODE#Product_CODE#Product Name#"> 
</div> 

我試圖找出是如何,我可以顯示

  • RefCode
  • 產品代碼
  • 產品名稱

在popup.html

我不看編輯HTML或以任何方式操縱它..它嚴格只顯示隱藏的HTML - 在一個易於閱讀的彈出窗口。

希望有道理..

在此先感謝。

UPDATE:

Popup.html

<html> 
<head> 
<script> 
function readIds() { 
    console.log('Send request to content script'); 
    document.getElementById("response").innerText = "Requesting..."; 
    chrome.tabs.getSelected(null,function(tab){ 
     chrome.tabs.sendRequest(tab.id, {cmd: "readIds"}, function(response){ 
     console.log('Response from page is:' + response); 
     document.getElementById("response").innerText = JSON.stringify(response); 
     }); 
    }); 
} 
</script> 
</head> 
<body style="width:400px;"> 
<a href="javascript:readIds();return false;">Click to read ids</a> 
<pre id="response"></pre> 
</body> 
</html> 

Content_script.js

// add a listener to get messages from background, popup 
chrome.extension.onRequest.addListener(function (request, sender, sendResponse) { 
     switch (request.cmd) { 
      case "readIds": 
       console.log("readIds", request); 
       document.getElementById("productID"); 
       sendResponse({refCode:1, productCode: 2, productName: 3}); 
       break; 
     } 
}); 

的manifest.json

{ 
    // Required 
    "name": "WP Debug", 
    "version": "0.0.1", 

    // Recommended 
    "description": "A plain text description", 
    "icons": { "48": "icon.png" }, 
    //"default_locale": "en", 

    // Pick one (or none) 
    "browser_action": { 
    "default_icon": "icon.png", // optional 
    "default_title": "WP Debug",  // optional; shown in tooltip 
    "popup": "popup.html" 
    }, 

    "permissions": [ "http://*/", "https://*/", "tabs" ], 

    "content_scripts": [ 
    { 
     "matches": ["http://*/*", "https://*/*"], 
     "js": ["content_script.js" ], 
     "run_at": "document_idle" 
    } 
    ] 
} 

回答

1

ÿ我們的彈出窗口需要向您的內容腳本發送消息,然後收集隱藏的字段信息並將響應發送回彈出窗口。

下面是一個例子:

popup.html

<html> 
<head> 
<script> 
function readIds() { 
    console.log('Send request to content script'); 
    document.getElementById("response").innerText = "Requesting..."; 
    chrome.tabs.getSelected(null,function(tab){ 
     chrome.tabs.sendRequest(tab.id, {cmd: "readIds"}, function(response){ 
      console.log('Response from page is:' + response); 
      document.getElementById("response").innerText = JSON.stringify(response); 
     }); 
    }); 
} 
</script> 
</head> 
<body style="width:400px;"> 
<a href="javascript:readIds();return false;">Click to read ids</a> 
<pre id="response"></pre> 
</body> 
</html> 

content_script.js

// add a listener to get messages from background, popup 
chrome.extension.onRequest.addListener(function (request, sender, sendResponse) { 
     switch (request.cmd) { 
      case "readIds": 
       console.log("readIds", request); 
       //TODO: Get real values to send from page 
       //e.g. document.getElementById("someid") etc 
       sendResponse({refCode:1, productCode: 2, productName: 3}); 
       break; 
     } 
}); 

mainfest.json

{ 
    // Required 
    "name": "Foo Extension", 
    "version": "0.0.1", 

    // Recommended 
    "description": "A plain text description", 
    "icons": { "48": "foo.png" }, 
    //"default_locale": "en", 

    // Pick one (or none) 
    "browser_action": { 
    "default_icon": "Foo.png", // optional 
    "default_title": "Foo Extension",  // optional; shown in tooltip 
    "popup": "popup.html" 
    }, 

    "permissions": [ "http://*/", "https://*/", "tabs" ], 

    "content_scripts": [ 
    { 
     "matches": ["http://*/*", "https://*/*"], 
     "js": ["content_script.js" ], 
     "run_at": "document_idle" 
    } 
    ] 
} 

見文檔:http://code.google.com/chrome/extensions/messaging.html

+0

感謝您的反饋和例子理查德將在未來幾天進行測試。 – 2012-04-18 09:59:34

+0

不用擔心。如果這回答你的問題,請標記爲答案。謝謝。 – 2012-04-18 10:45:57

+0

嗨Richard 我收到Uncaught SyntaxError:非法返回聲明\t popup.html:1 不知道如何處理?.. – 2012-04-23 23:04:55