Chrome扩展程序的活动标签和console.log [英] Chrome extension active tab and console.log

查看:189
本文介绍了Chrome扩展程序的活动标签和console.log的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个chrome扩展,我只想从一个简单的事情开始:抓取URL并将其显示在console.log()中.问题是当我单击该按钮时,什么都没有记录.

This is my first chrome extension and I just want to start with a simple thing: grabbing the url and showing it in console.log(). The problem is when I click that button, nothing is logged.

manifest.json

manifest.json

{
  "manifest_version": 2,

  "name": "Copy Title and URL",
  "description": "This extension will copy the title and url of an article",
  "version": "1.0",

  "browser_action": {
   "default_icon": "icon.png",
   "default_popup": "popup.html"
  },
  "permissions": [
   "activeTab"
   ]
}

popup.html

popup.html

<!doctype html>
<html>
  <head>
    <title>Copy Title & URL</title>
    <script src="popup.js"></script>
  </head>
  <body>
    <button id="copyUrl">Copy Title + URL</button>
  </body>
</html>

popup.js

document.addEventListener('DOMContentLoaded', function() {
  var copyURLButton = document.getElementById('copyUrl');
  copyURLButton.addEventListener('click', function() {
    chrome.tabs.getSelected(null, function(tab) {
      console.log(tab.url);
    });
  }, false);
}, false);

你知道我在做什么错吗?

Any idea what I'm doing wrong here?

谢谢.

推荐答案

您正在查找的控制台可能是错误的,因为您的代码看起来还不错(除了使用已弃用的getSelected()之外,您应该切换到query())

Probably the console you're looking in is wrong, as your code seems fine (except for using deprecated getSelected(), you should switch to query())

要访问弹出窗口的控制台,需要右键单击扩展程序的按钮,然后选择检查弹出窗口".

To access the popup's console, you need to right-click your extension's button and select "Inspect popup".

这篇关于Chrome扩展程序的活动标签和console.log的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆