Chrome扩展学习Demo:在popup里面显示我的 IP

Dear 丶 2024-02-19 22:12 57阅读 0赞

场景:查询ip地位地址

效果图:

20190819105921663.png

manifest.json

  1. {
  2. "manifest_version": 2,
  3. "name": "查看我的 IP",
  4. "version": "1.0",
  5. "description": "查看我的电脑当前的公网 IP",
  6. "icons": {
  7. "48": "icon.png"
  8. },
  9. "browser_action": {
  10. "default_icon": "icon.png",
  11. "default_popup": "popup.html"
  12. },
  13. "permissions": [
  14. "http://*/*", // 可以通过executeScript或者insertCSS访问的网站
  15. "https://*/*"// 可以通过executeScript或者insertCSS访问的网站
  16. ]
  17. }

popup.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8"/>
  5. <style>
  6. img {
  7. width: 500px;
  8. }
  9. </style>
  10. </head>
  11. <body style="width:500px;min-height:100px;">
  12. <div>这里是popup:</div>
  13. <div id="ip_div">正在查询……</div>
  14. <script type="text/javascript" src="popup.js"></script>
  15. </body>
  16. </html>

popup.js

  1. function httpRequest(url, callback) {
  2. var xhr = new XMLHttpRequest();
  3. xhr.open('GET', url, true);
  4. xhr.onreadystatechange = function () {
  5. if (xhr.readyState === 4) {
  6. callback(xhr.responseText);
  7. }
  8. };
  9. xhr.send();
  10. }
  11. httpRequest('http://pv.sohu.com/cityjson', function (ip) {
  12. document.getElementById('ip_div').innerText = ip;
  13. });

发表评论

表情:
评论列表 (有 0 条评论,57人围观)

还没有评论,来说两句吧...

相关阅读