使面板仅适用于某些 URL

发布于 2025-01-04 12:18:19 字数 2145 浏览 4 评论 0原文

Chrome 有一个名为“页面操作”的功能,我大致尝试使用 Firefox Addon SDK/Jetpack 复制该功能。可能有比我迄今为止尝试过的更好的方法,并且我愿意接受建议。

使用 tabs,我能够监听选项卡就绪并激活事件,如果选项卡的 URL 匹配,则应启用插件小部件;如果没有,则禁用。我已经可以在适当的时候更改图标,但我也想禁用该面板。

策略1:窃取点击事件并仅在我们位于正确页面时才显示面板;否则,忽略。问题是, 根据文档,手动显示面板会导致其无法锚定,一个没有太多进展的错误

策略2:禁用时将contentURL设置为null。收到一个错误,抱怨它不是 URL。

策略 3:针对禁用状态使用不同的 HTML 文档。将 panel.contentURL 设置为另一个 URL 在转到不同页面后不起作用?

这是代码:

const widgets = require("widget");
const Panel = require("panel").Panel;
const tabs = require("tabs");
const data = require("self").data;
const prefs = require("simple-prefs").prefs;

var panel = Panel({
    width: 480,
    height: 640,
    contentURL: data.url("panel.html"),
    contentScriptFile: [data.url('jquery.min.js'), data.url('panel.js')],
    onMessage: function (msg) { console.log(msg) }
});

var widget = widgets.Widget({
    id: "icon",
    label: "Export",
    contentURL: data.url("icon.png"),
    panel: panel
});


function enable() {
    widget.contentURL = data.url('icon.png');
    panel.contentURL = data.url("panel.html");
}

function disable() {
    widget.contentURL = data.url('icon_disabled.png');
    panel.contentURL = data.url("panel_disabled.html");
}

function on_change_tab(tab) {
    console.log(tab.url);
    if (/http:\/\/example.com\/.*/.exec(tab.url)) {
        console.log('ENABLE');
        enable();
    } else {
        console.log('DISABLE');
        disable();
    }
    console.log(panel.contentURL);
}
tabs.on('ready', on_change_tab);
tabs.on('activate', on_change_tab);

相关,但应该有锚定问题? 如何使用 firefox 插件重新加载小部件弹出面板SDK?

Chrome has something called "Page Actions", and I'm roughly trying to replicate that functionality with the Firefox Addon SDK/Jetpack. There's probably a better approach than what I've tried so far, and I'm open to suggestions.

Using tabs, I'm able to listen for tab ready and activate events, and if the tab's URL matches, the addon widget should be enabled; if not, disabled. I've got to the point where I can change the icon when appropriate, but I'd like to disable the panel as well.

Strategy 1: Steal the click event and only show the panel if we're on the right page; otherwise, ignore. Problem is, according to the docs, manually showing the panel causes it not to be anchored, a bug that's not had much progress on it.

Strategy 2: Set the contentURL to null when disabling. Get an error whining about it not being an URL.

Strategy 3: Use a different HTML document for the disabled state. Setting panel.contentURL to another URL doesn't work after going to a different page?

Here's the code:

const widgets = require("widget");
const Panel = require("panel").Panel;
const tabs = require("tabs");
const data = require("self").data;
const prefs = require("simple-prefs").prefs;

var panel = Panel({
    width: 480,
    height: 640,
    contentURL: data.url("panel.html"),
    contentScriptFile: [data.url('jquery.min.js'), data.url('panel.js')],
    onMessage: function (msg) { console.log(msg) }
});

var widget = widgets.Widget({
    id: "icon",
    label: "Export",
    contentURL: data.url("icon.png"),
    panel: panel
});


function enable() {
    widget.contentURL = data.url('icon.png');
    panel.contentURL = data.url("panel.html");
}

function disable() {
    widget.contentURL = data.url('icon_disabled.png');
    panel.contentURL = data.url("panel_disabled.html");
}

function on_change_tab(tab) {
    console.log(tab.url);
    if (/http:\/\/example.com\/.*/.exec(tab.url)) {
        console.log('ENABLE');
        enable();
    } else {
        console.log('DISABLE');
        disable();
    }
    console.log(panel.contentURL);
}
tabs.on('ready', on_change_tab);
tabs.on('activate', on_change_tab);

Related, but should have the anchoring problem? How to reload a widget popup panel with firefox addon sdk?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

在巴黎塔顶看东京樱花 2025-01-11 12:18:19

如果您仍然没有解决您的问题(以及其他有类似问题的人):

我有一个类似的问题并通过使用 erikvold 的 ToolbarButton 包。一旦你安装了它及其依赖项,你的 main.js 文件中的类似内容应该可以工作。

var pan = require("panel").Panel({
    width: 400,
    height: 600,
    contentURL: "http://stackoverflow.com"
    //maybe some more options here
});
var button = require("toolbarbutton").ToolbarButton({
    id: "myButton",
    label: "My Button",
    image: data.url("someicon.png"),
    panel: pan //This binds the panel to this toolbarbutton
});

我希望您能找到一些方法使其适应您的项目。

In case you still haven't solved your issue (and for anyone else having a similar problem):

I had a similar issue and resolved it by using erikvold's ToolbarButton package. Once you've installed that and its dependencies, something like this in your main.js file should work.

var pan = require("panel").Panel({
    width: 400,
    height: 600,
    contentURL: "http://stackoverflow.com"
    //maybe some more options here
});
var button = require("toolbarbutton").ToolbarButton({
    id: "myButton",
    label: "My Button",
    image: data.url("someicon.png"),
    panel: pan //This binds the panel to this toolbarbutton
});

I hope you can find some way to adapt this to your project.

写给空气的情书 2025-01-11 12:18:19

工具栏按钮.ToolbarButton({
id: "MyAddon",
标签:“我的插件”,
tooltiptext: "我的插件工具提示",
图片:data.url("logo.png"),
onCommand: 函数() {

         var dictionary_panel = require("panel").Panel({
         width:630,
         height:600,
         contentURL: data.url("HtmlPage.html"),
         contentScriptWhen: 'ready',
         contentScriptFile: [data.url("style.css"),data.url("jquery-1.7.1.js"),
                  data.url("javascriptquezz.js"),data.url("create.js")]          

});

        dictionary_panel.show();

}
});

toolbarbutton.ToolbarButton({
id: "MyAddon",
label: "My Addon",
tooltiptext: "My Addon Tooltip",
image: data.url("logo.png"),
onCommand: function() {

         var dictionary_panel = require("panel").Panel({
         width:630,
         height:600,
         contentURL: data.url("HtmlPage.html"),
         contentScriptWhen: 'ready',
         contentScriptFile: [data.url("style.css"),data.url("jquery-1.7.1.js"),
                  data.url("javascriptquezz.js"),data.url("create.js")]          

});

        dictionary_panel.show();

}
});

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文