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

时间:2022-10-07 16:36:39

I recently started learning addon-sdk plugin development and now stucked in a problem. In this plugin i have used togglebutton and panel for now. In panel i have loaded an html file that is "myform.html". When i fill the form i.e loaded in panel and click the submit button, with this, an ajax request sends the post data to the server and response returns. The ajax code is in a jquery file named "script.js". Now, After receiving the response i want to access current tab page components (eg. form, input type text etc.). when i use chrome objects like self, panel etc in ajax success function, it gives me errors. I searched alot to get rid of this error but nothing found useful for me. So tell me if i am going in a wrong direction or i am missing something in it.

我最近开始学习addon-sdk插件开发,现在陷入了一个问题。在这个插件中,我现在使用togglebutton和面板。在面板中我加载了一个“myform.html”的html文件。当我填写表格,即在面板中加载并单击提交按钮时,ajax请求将发布数据发送到服务器并返回响应。 ajax代码位于名为“script.js”的jquery文件中。现在,在收到响应后,我想访问当前标签页组件(例如表单,输入类型文本等)。当我在ajax成功函数中使用像self,panel等chrome对象时,它会给我带来错误。我搜索了很多,以摆脱这个错误,但没有发现对我有用。所以告诉我,如果我走向错误的方向,或者我错过了一些东西。

i am doing something like this (script.js):

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

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

1 个解决方案

#1


1  

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

您可以使用Panel()构造函数的contentScript或contentScriptFile选项指定一个或多个要加载到面板中的内容脚本。

You should ensure jQuery is included with your content scripts.

您应该确保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();

In your script.js you will not need to listen for the ready event as the contentScriptWhen ready.

在您的script.js中,您不需要像准备好内容脚本那样监听ready事件。

 // script.js
 $('#form').on('submit',function(login){
       $.ajax({
            ...
            ...
            success:function(result){
                 self.port.emit('ajaxComplete');
            }
       });
 });

Best of luck with your project

祝你的项目好运

#1


1  

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

您可以使用Panel()构造函数的contentScript或contentScriptFile选项指定一个或多个要加载到面板中的内容脚本。

You should ensure jQuery is included with your content scripts.

您应该确保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();

In your script.js you will not need to listen for the ready event as the contentScriptWhen ready.

在您的script.js中,您不需要像准备好内容脚本那样监听ready事件。

 // script.js
 $('#form').on('submit',function(login){
       $.ajax({
            ...
            ...
            success:function(result){
                 self.port.emit('ajaxComplete');
            }
       });
 });

Best of luck with your project

祝你的项目好运