Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/463.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 谷歌浏览器。扩展开发_Javascript_Google Chrome - Fatal编程技术网

Javascript 谷歌浏览器。扩展开发

Javascript 谷歌浏览器。扩展开发,javascript,google-chrome,Javascript,Google Chrome,我一整天都在努力,但什么都没有。为什么,请告诉我,这是为什么不想工作 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" }, "permis

我一整天都在努力,但什么都没有。为什么,请告诉我,这是为什么不想工作

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"]
      }
  ]
}
{
  "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"]
    }
  ]
}
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>
<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>
这让我看到:

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

天啊。我做到了。不要忘记将清单从“文件”更改为“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" : ["http://*/*"],
      "js": ["contentscript.js"]
      }
  ]
}
{
  "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

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

send();
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
 if (request.action == "getDOM")
   sendResponse({dom: document.body.innerHTML});
 else
   sendResponse({}); // Send nothing..
});
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>
<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>

身体
{
最小宽度:357px;
}
$(文档).ready(函数(){
chrome.tabs.getSelected(空,函数(选项卡){
//向内容脚本发送请求。
sendRequest(tab.id,{action:“getDOM”},函数(响应){
警报(response.dom);
});
});
});
你好,世界!

它看起来像
chrome.tabs
chrome.extensions
属性是
未定义的
。那么我应该怎么做才能让它工作呢?我的任务是获取当前选项卡的HTML源代码,但我读了很多消息,我应该“使用sendRequest和onRequest简单地传递数据”,但这并不简单,或者说我太笨了。我不知道wtf。