2012-04-16 31 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