2012-04-16 4 views
1

전 크롬 확장 세계가 처음입니다.팝업의 소스 코드 중 특정 라인 표시

튜토리얼 "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

Y 우리의 팝업은 숨겨진 필드 정보를 모아 팝업으로 응답을 보내는 콘텐츠 스크립트에 메시지를 보내야합니다. 여기

은 예입니다

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

피드백과 예제 Richard에게 감사드립니다. 앞으로 며칠 동안 테스트 해보십시오. –

+0

걱정할 필요가 없습니다. 귀하의 질문에 답변이 있으면 답변으로 표시하십시오. 감사. –

+0

안녕하세요 Richard 잡히지 않은 SyntaxError가 발생했습니다 : 잘못된 반환 문장 \t popup.html : 1 처리 방법이 확실하지 않습니까? –