我可以在 jquery 内容脚本文件(Firefox 插件 sdk)中使用插件代码吗?如果是怎么办?

Can i use addon code in jquery content script file (Firefox addon sdk)? if yes how?

最近开始学习addon-sdk插件开发,现在遇到了一个问题。在这个插件中,我现在使用了 togglebutton 和 panel。在面板中,我加载了一个 html 文件,即 "myform.html"。当我填写表格,即加载到面板中并单击提交按钮时,ajax 请求将 post 数据发送到服务器并响应 returns。 ajax 代码位于名为 "script.js" 的 jquery 文件中。现在,在收到响应后,我想访问当前的标签页组件(例如表单、输入类型文本等)。 当我在 ajax success 函数中使用 chrome 对象,如 self、panel 等时,它给我错误。我进行了大量搜索以消除此错误,但没有找到对我有用的东西。所以如果我走错了方向或者我遗漏了一些东西,请告诉我。

我正在做这样的事情(script.js):

$(document).ready(function(e) {
     $('#form').on('submit',function(login){
           $.ajax({
                .....
                .....
                success:function(result){
                     self.port.emit(...);
                }
           });
     });
});

You can specify one or more content scripts to load into a panel using the contentScript or contentScriptFile options to the Panel() constructor.

您应该确保 jQuery 包含在您的内容脚本中。

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

var panel = require("sdk/panel").Panel({
  contentURL: "https://en.wikipedia.org/w/index.php?title=Jetpack&useformat=mobile",
  contentScriptFile: [
    self.data.url("jquery.js"),
    self.data.url("script.js"),
  ]
  contentScriptWhen: 'ready',

});

panel.port.on('ajaxComplete', function (){
  console.log('completed!');
});

panel.show();

在您的 script.js 中,您不需要像 contentScriptWhen ready.

那样监听 ready 事件
 // script.js
 $('#form').on('submit',function(login){
       $.ajax({
            ...
            ...
            success:function(result){
                 self.port.emit('ajaxComplete');
            }
       });
 });

祝你的项目好运