【问题标题】:Get Activating browser tab atributes in firefox addon builder在 Firefox 插件生成器中获取激活浏览器选项卡属性
【发布时间】:2013-08-24 03:58:25
【问题描述】:

我正在尝试创建一个引导的 firefox 扩展来获取当前选项卡的 url 和标题。问题是我想检查标签网址何时在其网址中包含“about:”一词。我建议的解决方案是使用浏览器本机 string.substr() javascript 函数检查 require("sdk/tabs").activeTab.url"。是否可以在小部件或 ToolbarButton onClick 方法上使用浏览器本机 javasript 函数?

exports.main = function(options) {

  var base64 = require("sdk/base64");

  var panel = require("sdk/panel").Panel({
      width: 700,
      height: 470,
      onHide: function ()
      {
         panel.contentURL = "about:blank";
      }
  });

  var tbb = require("toolbarbutton").ToolbarButton({
      id: "extension-tbb-id",
      label: "IFShare+",
      image: "https://www.fasdfasd.es/factory/templates/templateforidkreader/favicon.ico",
      panel: panel,
      onClick: function()
      {
         windowPanel = require("sdk/tabs").activeTab;

         title = windowPanel.title;
         url = windowPanel.url; 
         // Is any posibility to do something like that ????
         contentScript: "if("+url+".substring(0,5)=='about:'){"
         {
            url='';
            title='';
         }
        contentScript: "}"

        this.panel.contentURL = "https://www.fasdfasdf.es/factory/index2.php?option=com_idkreader&view=shareplus&task=window&Itemid=0&url="+base64.encode(url, "utf-8")+'&title='+base64.encode(title, "utf-8")+'&ref=ext';      
      },

    });


  // move the toolbar button to the navigation bar
  if ("install" == options.loadReason) {
     tbb.moveTo({toolbarID: "nav-bar"});
   }
}

【问题讨论】:

    标签: firefox-addon firefox-addon-sdk


    【解决方案1】:

    我不完全清楚你想要完成的细节,但我认为你已经接近了。

    要按要求回答问题,不,您不能从内容脚本中访问变量。您能做的最好的事情是使用the page-mod documentation 中提到的内容脚本消息传递。

    但是,要完成您想要的,您不需要这样做。你可以在 onClick 函数本身中做你想做的事,就像这样

    function onClick()
      {
         windowPanel = require("sdk/tabs").activeTab;
    
         title = windowPanel.title;
         url = windowPanel.url; 
    
         if(url.substring(0,5)=='about:'){
         {
            url='';
            title='';
         } else {
             //You can insert any content script stuff you want here
         }
        this.panel.contentURL = "https://www.fasdfasdf.es/factory/index2.php?option=com_idkreader&view=shareplus&task=window&Itemid=0&url="+base64.encode(url, "utf-8")+'&title='+base64.encode(title, "utf-8")+'&ref=ext';      
      }
    

    如果您稍微完善您的问题,我会很乐意完善我的答案。

    【讨论】:

    • 感谢您的帮助,这真的很有用。您或多或少地回答了我的问题。现在我对插件创建和 JavaScript 有了更多的了解。
    猜你喜欢
    • 2015-11-25
    • 2013-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-26
    • 1970-01-01
    • 2015-08-26
    • 1970-01-01
    相关资源
    最近更新 更多