gpt4 book ai didi

javascript - 为什么我的 init() 函数没有运行? (安装 Firefox 扩展时自动添加工具栏按钮,但仅在首次运行时)

转载 作者:行者123 更新时间:2023-12-01 15:29:05 24 4
gpt4 key购买 nike

我不断地回到这个问题,并且一直未能解决它。

我已经阅读了数十页描述如何做到这一点,并查看了诸如 How can I make my Firefox extension toolbar button appear automatically? 之类的答案。在这里,但我就是无法做到这一点。

我了解我需要做什么的概念,但我已经达到的一点是,虽然扩展程序本身可以正常工作,但我无法获得 init( ) 函数来运行,并且似乎也没有添加负载监听器(我在 alert 代码之前和之后添加了 window.top.addEventListener.. ,但 alert 也没有触发。

我想要实现的是在安装/更新时添加工具栏按钮的推荐过程,但仅在第一次运行/安装时。

我的扩展包括:

  • chrome.manifestinstall.rdf在扩展根目录中。
  • 一个 chrome包含其他元素的文件夹( extensionName.jsextensionName.pngextensionName.xulextensionName.cssicon.png )。
  • prefs.jsdefaults/preferences目录。这包含 extensions.MyExtensionNameButton.firstRunDone首选项设置为 false .

  • 扩展本身完全按照要求工作(它包括一个工具栏按钮和右键单击上下文菜单项)。

    我的 extensionName.js文件包含以下内容(实际的扩展功能在它们工作时会被 trim 掉,并且会使问题变得太长):
    objMyExtensionNameButton = {

    // functions that are called by the .xul are in here,


    init : function() {
    var firstRunPref = "extensions.MyExtensionNameButton.firstRunDone";
    alert("init function run");
    if (!Application.prefs.getValue(firstRunPref)) {
    Application.prefs.setValue(firstRunPref, true);
    // all the rest of the first run code goes here.
    alert("this is the first run");
    } else {
    alert("this is NOT the first run");
    }
    }
    };

    window.top.addEventListener("load", function() { objMyExtensionNameButton.init(); }, false);

    包含 .js 文件的 xul 的相关部分如下所示:
    <?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet type="text/css"
    href="chrome://MyExtensionNameButton/content/button.css"?>

    <!DOCTYPE overlay >
    <overlay id="MyExtensionNameButton-overlay"
    xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

    <script type="application/javascript"
    src="chrome://MyExtensionNameButton/content/MyExtensionNameButton.js"/>

    <!-- Firefox -->
    <toolbarpalette id="BrowserToolbarPalette">
    <toolbarbutton id="MyExtensionNameButton"/>
    </toolbarpalette>


    <!-- button details -->
    <toolbarbutton id="MyExtensionNameButton"

    ...etc etc etc...

    安装扩展程序或 Firefox 启动时,我似乎无法运行任何东西 - 单击工具栏按钮或右键菜单中的扩展程序选项可以毫无问题地运行这些功能 - 这些功能都包含在一个对象中。

    我试过移动 init()在该对象的内部和外部都起作用,并且我相信正确地调用它。

    另一个奇怪的事情是,我似乎没有在控制台的扩展中看到任何错误(当我没有将 default/preferences/prefs.js 文件添加到 .xpi 时,我也没有看到任何错误,所以似乎有些错误那里也有问题)。

    谢谢,

    调频

    最佳答案

    在您的默认 prefs.js ,你应该添加

    pref('extensions.MyExtensionNameButton.firstRunDone', false);

    由于首选项 extensions.MyExtensionNameButton.firstRunDone不存在,调用 Application.prefs.getValue(firstRunPref)应该失败了。

    如果您打开错误控制台 Ctrl+Shift+j,您应该会看到相关错误。

    关于javascript - 为什么我的 init() 函数没有运行? (安装 Firefox 扩展时自动添加工具栏按钮,但仅在首次运行时),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20403784/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com