2012-07-20 24 views
13

我正在做一個Firefox瀏覽器擴展程序,需要查找當前選項卡查找當前選項卡的URL。製作Firefox瀏覽器插件

我試過這個帖子Opening a URL in current tab/window from a Firefox Extension的URL,但它告訴我,「窗口」是不是定義。 (我想是因爲我在做一個附加,而不是擴展名)

這裏就是我試圖做的事:

var widgets = require('widget'); 
var tabs = require('tabs'); 

var widget1 = widgets.Widget({ 
    id: "widget1", 
    label: "widget1", 
    contentURL: "http://www.mozilla.org/favicon", 
    onClick: function() { 
    console.log(tabs.url); 
    } 
}) 

我做了一個小工具,例如,當我點擊它當前標籤的網址應該是'console.log'。

似乎並沒有發生!繼續收到「info:undefined」,這顯然意味着tabs.url沒有返回任何東西。但這似乎是使用它的方式https://addons.mozilla.org/en-US/developers/docs/sdk/1.5/packages/addon-kit/docs/tabs.html

任何人有什麼想法?

感謝,

請問

回答

11

就快:

const { ActionButton } = require("sdk/ui/button/action"); 
const clipboard = require("sdk/clipboard"); 
const tabs = require('sdk/tabs'); 

let button = ActionButton({ 
    id: "my-button-id", 
    label: "Button Label", 
    icon: { 
    "32": "chrome://mozapps/skin/extensions/extensionGeneric.png" 
    }, 
    onClick: function(state) { 
    let url = tabs.activeTab.url; 
    console.log("active tab url:", url); 
    require("sdk/notifications").notify({ 
     title: "Active Tab's Url is "+url, 
     text: "Click to copy.", 
     onClick: function() { 
     clipboard.set(url); 
     } 
    }); 
    } 
}); 

你應該看看documentation on the tabs module

注意:我已更新此代碼示例以使用自Firefox 29以來提供的新UI模塊 - 原始問題中使用的「窗口小部件」模塊當時有效,但此後已被棄用,然後被刪除。

+0

ahh非常感謝。也將看看噴氣揹包的例子。 – WillJones 2012-07-29 22:49:40

相關問題