如何阅读在线资源并更新Google Chrome扩展程序中的弹出窗口

史蒂文·诺布尔

我正在尝试创建我的第一个Google Chrome扩展程序。

有什么想法为什么不将它从example.com网站复制粘贴到我的弹出窗口?

popup.js:

document.addEventListener('DOMContentLoaded', function () {
  var xhr = new XMLHttpRequest();
  xhr.open("GET", chrome.extension.getURL('http://example.com'), false);
  xhr.send();
  document.getElementById('response').innerHTML = xhr.responseText;
});

popup.html:

<!doctype html>
<html>
  <head>
    <script type="text/javascript" src="popup.js"></script>
  </head>
  <body>
    <p id="response"></p>
  </body>
</html>

manifest.json:

{
  "manifest_version": 2,
  "name": "Getting started",
  "description": "Display test from Example.com",
  "version": "1.0",
  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html",
  },
  "permissions": [
    "http://example.com"
  ]
}

顺便说一句,我不打算将捕获的内容直接插入到我的弹出窗口中!(脚本注入等。)在这个阶段,我只是在研究一切工作原理。

吉富

有几个问题

  1. 您需要为您发出的任何XMLHttp请求请求权限,因此扩展名中清楚地标记了该权限。这样做的方法是在manifest.son中列出要访问的网站。这非常简单,在这里介绍:https : //developer.chrome.com/extensions/xhr

  2. 请求本身,当您发送它时,它没有立即答复。这是因为它正在通过Internet传输,并在后台发出您的请求。因此,您不能在致电send之后直接请求responseText,因为它尚不知道答案。您必须告诉它在完成加载后再调用一个函数。这称为回调。

  3. 调试。您可以从Google安装一种工具,以在扩展程序上使用“检查元素”。这意味着您可以右键单击下拉菜单,然后从上下文菜单中选择“检查元素”。然后,您可以转到控制台标签并查看所有JavaScript错误。https://chrome.google.com/webstore/detail/chrome-apps-extensions-de/ohmmkhmmmpcnpikjeljgnaoabkaalbgc?hl=zh-CN

这也意味着您可以将console.log(“ am here”)撒在任何地方,并且可以看到正在调用的内容和未调用的内容...

这是我将您的内容与Google示例结合在一起的结果。

希望这可以帮助!吉富

manifest.json

{
  "manifest_version": 2,

  "name": "Getting started example",
  "description": "This extension shows a Google Image search result for the current page",
  "version": "1.0",

  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "permissions": [
    "activeTab",
    "http://www.example.com/"
  ]
}

Popup.js

// makeWebRequest
//
// ANY URLs you want to call with "makeWebRequest" need to be listed in your manifest.json
//    https://developer.chrome.com/extensions/xhr

//
// responseType 'json' 'document' 'text'
// callback is the function to call back on success. will be passed the XMLHttpRequest
// errorCallback is called when there's a problem

function makeWebRequest(urlToLoad, responseType, callback, errorCallback) {

  console.log("making web request for: "  + urlToLoad);

  var xmlReq = new XMLHttpRequest();
  xmlReq.open('GET', urlToLoad);
  // The Google image search API responds with JSON, so let Chrome parse it.
  xmlReq.responseType = responseType;
  xmlReq.onload = function() {
    // Parse and process the response from Google Image Search.
    var response = xmlReq.response;
   /// if (!response || !response.responseData || !response.responseData.results ||
    //    response.responseData.results.length === 0) {

    if (!xmlReq.response) {
       errorCallback('No response from the web request!');
       return;
    }


    callback(xmlReq);
  };
  xmlReq.onerror = function() {
    errorCallback('Network error.');
  };
  xmlReq.send();  // this goes away, makes the internet request, then calls back when it's finished.
}




// This is called when the extension loads.

document.addEventListener('DOMContentLoaded', function () {

  // Response type:
  // use json for json, document for html documents, text for raw text

  makeWebRequest("http://www.example.com/index.html", "document", 
    function (xmlReq) {
        // this is the function that is called back on when "Send" finishes

        // returns an HTMLDocument because we passed the "document" 
        // responseType into makeWebRequest.
        var doc = xmlReq.response;

        console.log("web request finished " + xmlReq.response);

        document.getElementById('response').innerHTML = doc.documentElement.innerHTML;
    }

  , function(errorMessage) {
        // this is the function that is called back on when there is an error 
        // requesting the file
        console.log("got error:" + errorMessage);

  });

});

Popup.html

<!doctype html>
<!--
 This page is shown when the extension button is clicked, because the
 "browser_action" field in manifest.json contains the "default_popup" key with
 value "popup.html".
 -->
<html>
  <head>
    <title>Getting Started Extension's Popup</title>
    <style>
      body {
        font-family: "Segoe UI", "Lucida Grande", Tahoma, sans-serif;
        font-size: 100%;
      }
      #status {
        /* avoid an excessively wide status text */
        white-space: pre;
        text-overflow: ellipsis;
        overflow: hidden;
        max-width: 400px;
      }
    </style>

    <!--
      - JavaScript and HTML must be in separate files: see our Content Security
      - Policy documentation[1] for details and explanation.
      -
      - [1]: https://developer.chrome.com/extensions/contentSecurityPolicy
     -->
    <script src="popup.js"></script>
  </head>
  <body>
    Sample extension
    <div id="status"></div>
    <p id="response"></p>
    <img id="image-result" hidden>
  </body>
</html>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Google Chrome扩展程序中启动新窗口

如何为Chrome扩展程序添加粘性弹出窗口

关闭和打开Chrome扩展程序弹出窗口时如何不丢失数据

如何确定内容脚本的弹出窗口中是否包含Chrome扩展程序?

如何缩小Chrome扩展程序的弹出窗口

Chrome扩展程序在页面操作中显示弹出窗口

如何让Mailchimp弹出窗口在Chrome中工作

如何在Chrome扩展程序弹出窗口上创建“ tail”

如何使用Chrome扩展程序popup.html从新的弹出窗口中定位DOM元素

如何使Chrome扩展程序弹出窗口可拖动?

在chrome扩展程序中打开和访问弹出窗口

如何在Chrome扩展程序中仅显示一次弹出窗口?

如何在Chrome扩展程序中显示弹出式窗口?

如何从Chrome扩展程序中打开的窗口获取背景页面?

Google Chrome扩展程序:始终可见的弹出窗口

Google Chrome扩展程序在弹出窗口和选项卡之间进行对话

如何防止我的Chrome扩展程序中的弹出窗口放大

Chrome扩展程序:如何在页面加载时运行Javascript(不打开弹出窗口)

如何阻止亚马逊上的Chrome扩展程序弹出窗口

如何跟踪哪个chrome扩展程序会生成弹出框?

Google Chrome扩展程序:模态/弹出窗口关闭时发出警报

在Google Chrome浏览器扩展程序弹出窗口中发送异步消息

如何防止Chrome扩展程序在同一弹出窗口仍处于打开状态时重新打开它?

Chrome 扩展程序:如何通过单击 html 弹出窗口中的按钮在新选项卡中打开指定链接?

在 Google Chrome 扩展程序中获取打开的弹出窗口

在 chrome 扩展程序中创建一个弹出窗口

每次加载页面时,如何从后台向 Chrome 扩展程序中的弹出窗口发送消息?

如何使我的 Chrome 扩展程序只需单击一次即可激活默认弹出窗口?

如何仅更改带有弹出窗口的活动标签的 Chrome 扩展程序图标?