Firefox WebExtension:找不到错误

时间:2018-12-17 03:18:13

标签: javascript firefox firefox-addon

我了解如何运行扩展调试器,但我不了解错误消息。我想我会做的是发布我的所有代码,大致如此,并希望有人能提供帮助。我是一个经验丰富的程序员,但是WebExtensions似乎很难学习。到目前为止,我已经花了几天的时间来尝试示例(可以工作)并构造自己的代码(通常不起作用)。

这些评论解释了我正在尝试做的事情,它基本上是在尝试创建一个后台和前台框架,并最终在其中创建一个密码管理器。

manifest.json:

num

PM_bg.js:

{
  "manifest_version": 2,
  "name": "PwdMgr",
  "description": "User tool to manage passwords and log into websites.",
  "version": "0.2",
  "icons": {
    "48": "icons/link-48.png"
  },

"permissions": [
    "<all_urls>",
    "activeTab",
    "contextMenus",
    "notifications"
],

  "background": {
    "scripts": ["PM_bg.js"]
  }
}

PM_fg.js:

// S: Promise to show string in notification box
function S(Msg,Title)
    {
    var p=browser.notifications.create({
        "type": "basic",
        "iconUrl": browser.extension.getURL("icons/link-48.png"),
        "title": Title,
        "message": Msg});
    return p;
    } // S

S('bg running','Direct from bg').then(empty).catch(err);

// Receive and show fg msgs
browser.runtime.onMessage.addListener(function(message)
    {
    if (message.msg)
        S(message.msg,'Msg from fg').then(empty).catch(err);
    if (message.clickmsg)
        S(message.clickmsg,'Click Msg from fg').then(empty).catch(err);
    });

// Create context menu item
browser.contextMenus.removeAll().then(p2).catch(err);

function p2()
    {
    browser.contextMenus.create(
        {
        id: "ServInt",
        title: "ServInt Reports"
        }, function()
        {
        if (browser.runtime.lastError)
            err(browser.runtime.lastError);
        }); // Sync
    } // p2

// Create context menu command to run PM_fg.js

browser.contextMenus.onClicked.addListener(function(info, tab)
    {
    if (info.menuItemId == "ServInt")
        {
        var p=browser.tabs.executeScript({file:"/PM_fg.js"}).then(fgReady).catch(err);
        }
    }).then(empty).catch(err);

// FG is ready
function fgReady(Result)
    {
    S('browser.tabs.executeScript is done; FG is running','msg from BG').then(empty).catch(err);
    // Send message to fg
    //browser.runtime.sendMessage({"msg":"from background"});
    } // fgReady

// BG unloading: delete context menu item

window.addEventListener('unload',function(e)
    {
// This is never run!
    browser.menus.removeAll().then(empty).catch(err);
    S('removed menu items','Direct from bg').then(empty).catch(err);
    console.log('removed menu items');
    });

/*
//var gettingCurrent = browser.tabs.getCurrent()
browser.tabs.sendMessage(
  0,
  {msg:"from background"}).then();
*/

// Functions

function p1()
    {
    console.log('Notification done (bg)');
    } // p1

function empty(ignored)
    {
    console.log('doing nothing');
    } // empty

function err(Msg)
    {
    console.log('*** '+Msg);
    throw Msg;
    } // err
// End

0 个答案:

没有答案