简体   繁体   English

在ActionButton上发送HTTP请求onClick-Firefox插件

[英]Send HTTP Request onClick on ActionButton - Firefox addon

What's my outcome? 我的结果是什么? I am creating an addon and I want to send a HTTP request when the ActionButton is clicked. 我正在创建一个插件,我想在单击ActionButton时发送一个HTTP请求。 The data that will be transfered to the server include the URL of the tab and the content of the page. 将要传输到服务器的数据包括选项卡的URL和页面的内容。

I tried to create a log message and grabbing it from the console(listener). 我试图创建一条日志消息,并从控制台(监听器)中获取它。 Turns out that doesn't work at all. 原来那根本不起作用。 Secondly, I tried to inject(pageMod) contentScript into the website when the button is clicked. 其次,当单击按钮时,我尝试将contentpageScript(pageMod)注入网站。 It doesn't work that well too. 它也不是很好。 The code is executed several times instead of once. 该代码执行几次,而不是执行一次。

Code: main.js 程式码:main.js

var button = buttons.ActionButton({
 id: "bookmark-link",
 label: "Bookmark this website",
 icon: {
   "16": "./status-bar.png",
   "32": "./toolbar-large.png",
   "64": "./toolbar-large.png"
 },
 onClick: function() {

     log("triggered!"); // I need to make the Ajax GET request here

 }

});

This problem kept me awake the whole night and I can't find any solution that works great. 这个问题使我整夜都无法入睡,我找不到任何有效的解决方案。 So I try to ask for help here. 所以我尝试在这里寻求帮助。 I appreciate your time! 感谢您的宝贵时间!

Thank you. 谢谢。

I recommend using the request module in your onClick handler. 我建议在onClick处理程序中使用request模块。

If you really want to use a content script then do: 如果您确实要使用内容脚本,请执行以下操作:

onClick: function() {
    require('sdk/tabs').activeTab.attach({
      contentScriptFile: require('sdk/self').data.url('request-maker.js'),
    })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM