无法显示 Firefox 扩展日志



按照这些说明,我转到about:debugging->This Firefox并在我的扩展上单击Inspect,它显示了一个控制台。但当我触发我的扩展时,没有日志显示在那里。使用列表cookie示例,我添加了最后两行:

gettingAllCookies.then((cookies) => {
//set the header of the panel
var activeTabUrl = document.getElementById('header-title');
var text = document.createTextNode("Cookies at: "+tab.title);
var cookieList = document.getElementById('cookie-list');
console.log('I can't see this log!');
cookieList.parentNode.appendChild(document.createTextNode(Date()));

当我调用弹出窗口时,我在弹出窗口中看到当前日期/时间,但控制台中没有显示日志。

我试着按照这里提到的设置extensions.sdk.console.logLevel并重新启动(尽管我认为这是针对旧版本的(,但没有帮助。

我想可能有控制台权限或我可能需要添加到清单中的东西,但没有找到任何这样的东西。

完整的代码供参考。我只更改了标有+/-的行:

function showCookiesForTab(tabs) {
//get the first tab object in the array
let tab = tabs.pop();
//get all cookies in the domain
var gettingAllCookies = browser.cookies.getAll({url: tab.url});
gettingAllCookies.then((cookies) => {
//set the header of the panel
var activeTabUrl = document.getElementById('header-title');
var text = document.createTextNode("Cookies at: "+tab.title);
var cookieList = document.getElementById('cookie-list');
-   activeTabUrl.appendChild(text);
+
+   console.log('I can't see this log!');
+   cookieList.parentNode.appendChild(document.createTextNode(Date())); // I see this updated even though I don't see the log

if (cookies.length > 0) {
//add an <li> item with the name and value of the cookie to the list
for (let cookie of cookies) {
let li = document.createElement("li");
let content = document.createTextNode(cookie.name + ": "+ cookie.value);
li.appendChild(content);
cookieList.appendChild(li);
}
} else {
let p = document.createElement("p");
let content = document.createTextNode("No cookies in this tab.");
let parent = cookieList.parentNode;
p.appendChild(content);
parent.appendChild(p);
}
});
}
//get active tab to run an callback function.
//it sends to our callback an array of tab objects
function getActiveTab() {
return browser.tabs.query({currentWindow: true, active: true});
}
getActiveTab().then(showCookiesForTab);

Firefox控制台被划分为不同的区域。console.log()的结果可以在相对区域中查看。

  • 多进程浏览器控制台Ctrl+Shift+J
    主要由Firefox自己记录
  • Web开发人员工具Ctrl+Shift+IF12
    按选项卡/网页和插件的内容脚本记录
  • 扩展工具箱关于:调试#/runtime/this firefox➜XYZaddon➜通过XYZaddon的后台脚本检查日志

更新

根据评论,这里有一个经过测试的简化代码,您可以使用它。日志显示在扩展工具箱上。

async function showCookiesForTab() {
// get the first tab object in the array
const tabs = await browser.tabs.query({currentWindow: true, active: true});
// get all cookies in the domain
const cookies = await browser.cookies.getAll({url: tabs[0].url});
console.log(cookies);
// Array(6) [ {…}, {…}, {…}, {…}, {…}, {…} ]
}
showCookiesForTab();

我也遇到了类似的问题。我没有弄清楚原因,但我找到了在扩展工具箱中查看console.log的方法。

我添加了一个后台脚本来处理popup.js的大部分逻辑。由于有一个后台脚本在运行,我可以看到日志。

仍然不知道为什么我一开始就看不到日志。

最新更新