【问题标题】:Opening my page in firefox after installing the addon安装插件后在 Firefox 中打开我的页面
【发布时间】:2011-01-29 09:17:01
【问题描述】:

嗨,

firefox 安装后第一次重新启动后,我试图打开我的主页。

为此,我在加载页面上添加事件处理程序,并检查此事件第一次执行的位置。

window.addEventListener("load", initializeOverlay, false);

但我的问题是如何在 firefox 启动时在新选项卡中打开页面。我用

`window.open("https://www.xyz.com/");`

但这会在新窗口中打开页面,甚至可能在 Internet Explorer 中打开。

那么有什么方法可以在即将打开的同一窗口中的新标签页中打开页面。

谢谢 BHAVIK GOYAL

【问题讨论】:

    标签: javascript firefox-addon


    【解决方案1】:

    我设法使用首选项来做类似的事情,而不是创建文件等。

    /defaults/preferences/default.js 内部:

    pref("extensions.extension_name.just_installed", true);
    pref("extensions.extension_name.post_install_url", "http://www.google.com");
    

    然后在插件的主 JS 文件中:

    // Retrieve the preferences.
    var prefs;
    prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("extensions.extension_name.");
    prefs.QueryInterface(Components.interfaces.nsIPrefBranch2);
    
    // If we just installed, open the post-install page and update the preferences.
    var just_installed = prefs.getBoolPref("just_installed");
    var post_install_url = prefs.getCharPref("post_install_url");
    if (just_installed) {
      prefs.setBoolPref("just_installed", false);
      gBrowser.selectedTab = gBrowser.addTab(prefs.getCharPref("post_install_url"));    
    }
    

    唯一的问题是 Firefox 在卸载扩展程序后不会重置该扩展程序保存的首选项。

    【讨论】:

    • 感谢 NudeCanalTroll,但这是否适用于所有操作系统,我的意思是 Mac、Linux 和 Windows?
    • This solution 使用最新的 Firefox 附加 SDK 对我来说效果更好。
    【解决方案2】:

    我得到了答案。我们可以添加gbrowser.open("http://www.xyz.com/") 以在新选项卡中打开,并且该语句必须在新函数中执行,即调用另一个事件处理函数loadedoverlay,其定义如下:

    function loadedOverlay() {
    try{
     var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
     file.initWithPath(Components.classes["@mozilla.org/file/directory_service;1"].getService( Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsIFile).path+"\\initialstart.txt");
     if ( file.exists() == true )
    {   
    }
    else
    {
    file.create( Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 420 );
    var Website="http://www.emailstationery.com/";
    gBrowser.addTab(Website);//This is for new Tab
    }
    } catch(e) {}
    }
    

    必须在加载事件函数中添加对该函数的调用,方法是添加如下代码行:-

    var appcontent = document.getElementById("appcontent");   // browser 
    if(appcontent) 
        appcontent.addEventListener("DOMContentLoaded", loadedOverlay, true); 
    

    【讨论】:

    • 请注意,创建一个文件来跟踪它是多余的;设置首选项要容易得多。否则我会指出这不是你应该使用文件名的方式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-31
    • 1970-01-01
    • 1970-01-01
    • 2018-02-13
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多