简体   繁体   English

谷歌浏览器。 延展剂

[英]Google Chrome. Extension develompent

All day long i'm trying to make this work, but nothing. 我整天都在努力使这项工作有效,但一无所获。 Why, tell me pls, why is this #@!*> dont want to work? 为什么,告诉我,为什么这个#@!*>不想工作?

Manifest.json 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 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 contentscript.js

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

send();

This show me: 这告诉我:

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

Thx, god. 天哪,天哪。 I did it. 我做的。 Dont forget change manifest from "file" to "http". 不要忘记将更改清单从“文件”更改为“ http”。

manifest.json 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 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 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM