2011-02-01 16 views
0

私はこの仕事をしてみようとしていますが、何もありません。なぜ、私に教えてください、なぜこの#@!*>は働きたくないのですか?Google Chrome。拡張develompent

manifest.jsonを

{ 
    "name": "My First Extension", 
    "version": "1.0", 
    "description": "The first extension that I made.", 
    "browser_action": { 
    "default_icon": "icon.png", 
    "popup": "popup.html" 
    }, 
    "permissions": [ 
    "tabs" 
    ], 
    "content_scripts": [ 
     { 
     "matches" : ["http://*/*"], 
     "js": ["contentscript.js"] 
     } 
    ] 
} 

popup.html

<script src="contentscript.js"></script> 
<script> 

function get(){ 
chrome.extension.onRequest.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"}); 
    else 
     sendResponse({}); // snub them. 
    }); 
} 

get(); 

</script> 

contentscript.js

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

send(); 

このSどうすればいいですか:

Uncaught TypeError: Cannot call method 'getSelected' of undefined 
Uncaught TypeError: Cannot read property 'onRequest' of undefined 
+0

それは 'chrome.tabs'と'のように見えますクロム.extensions'プロパティは 'undefined'です。 –

+0

これを行うにはどうすればいいですか? – FSou1

+0

私の仕事は現在のタブのHTMLソースを取得するが、私は "sendRequestとonRequestで単純なデータを渡す"というメッセージをたくさん読んでいますが、それは本当に単純ではないです。 wtfは分かりません。 – FSou1

答えて

1

Thx、神。やったよ。 "ファイル"から "http"への変更マニフェストを忘れないでください。

manifest.jsonを

{ 
    "name": "My First Extension", 
    "version": "1.0", 
    "description": "The first extension that I made.", 
    "browser_action": { 
    "default_icon": "icon.png", 
    "popup": "popup.html" 
    }, 
    "permissions": [ 
    "tabs" 
    ], 
    "content_scripts": [ 
    { 
     "matches": ["file:///*"], 
     "js": ["dom.js"] 
    } 
    ] 
} 

dom.js

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) { 
if (request.action == "getDOM") 
    sendResponse({dom: document.body.innerHTML}); 
else 
    sendResponse({}); // Send nothing.. 
}); 

popup.html

<html> 
<head> 
    <style type="text/css"> 
     body 
     { 
      min-width: 357px; 
     } 
    </style> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script> 
    <script type="text/javascript" language="javascript"> 
     $(document).ready(function() { 
      chrome.tabs.getSelected(null, function (tab) { 
       // Send a request to the content script. 
       chrome.tabs.sendRequest(tab.id, { action: "getDOM" }, function (response) { 
        alert(response.dom); 
       }); 
      }); 
     }); 
    </script> 
</head> 
<body> 
    <h4>Hello, world!</h4> 
</body> 
</html> 
関連する問題