无法让Chrome popup.js使用console.log

gda*_*nko 41 javascript google-chrome-extension

我在编写JS时大量使用console.log进行调试.我正在尝试使用它来编写chrome扩展,但它无法正常工作.这里有一些诡计吗?

popup.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
    <link type="text/css" rel="stylesheet" href="css/jquery-ui-1.10.0.custom.min.css" />
    <script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
    <script type="text/javascript" src="js/jquery-ui-1.10.0.custom.min.js"></script>
    <script type="text/javascript" src="js/popup.js"></script>
</head>

<body style="width: 200px">
</body>
Run Code Online (Sandbox Code Playgroud)

popup.js

console.log('test1');
$(document).ready(function() {
    console.log('test2');
});
Run Code Online (Sandbox Code Playgroud)

这些都不会出现在JS调试器中.

San*_*kar 88

我最初也有这个问题!确保打开了正确的开发人员工具窗口...我的意思是,您可能已经打开了主页面的开发人员工具窗口而不是扩展程序页面(即popup.html).

要打开用于检查弹出窗口的开发人员工具窗口,请右键单击弹出窗口,然后单击"检查元素"...这将打开正确的开发人员工具窗口.

我最初犯了这个愚蠢的错误并被卡住了.. :)

  • @gdanko`chrome.extension.getBackgroundPage().console.log(...)`有效,除了你还需要向你的后台页面发送异常.`try {} catch {}``和`throw`到你的背景页面,至少知道它们发生了. (4认同)