简体   繁体   English

FireFox扩展SDK将GM_xmlhttprequest转换为与插件sdk兼容的东西

[英]FireFox Extension SDK converting GM_xmlhttprequest to somthing compatible with the addon sdk

I started with a grease monkey script. 我从油脂猴子脚本开始。 there you use the command GM_xmlhttprequest and are able to get cross domain data. 在这里,您可以使用命令GM_xmlhttprequest并能够获取跨域数据。 I originally had the code below working in Greasemonkey, now that i have grown beyond GM capibilitys I need to move it over to the addon SDK. 我最初在Greasemonkey中使用下面的代码,现在我已经超越了GM的功能,因此需要将其移至附加SDK。

GM_xmlhttpRequest({
      method: 'POST',
      url: "http://10.1.1.1/app/site_search_info.php",
      headers: {
             "Content-Type": "application/x-www-form-urlencoded"
          },
      data: encodeURI("link=" + itemDir[2]),
      onload:function(r)
               {
               ................ original html formating code here...
               }
 });

I was able to pull the date information about each rows item and then have it append that information behind the current text in each row. 我能够提取有关每行项目的日期信息,然后将其附加到每行当前文本的后面。 i used a pagemod call in main.js to load my search.js which contians this code. 我在main.js中使用了pagemod调用来加载包含此代码的search.js。 when I get a page that should execute the xmlhttprequest it does not show an error in the browsers error console and does not run. 当我得到一个应该执行xmlhttprequest的页面时,它不会在浏览器错误控制台中显示错误并且无法运行。 putting an alert just before the XHR does appear. 在XHR确实出现之前发出警报。 Since it is a cross site XHR i can only assume that either i got the syntax wrong when trying to replace GM_xmlhttprequest, or it is being blocked by security settings. 由于它是跨站点XHR,因此我只能假定要么尝试替换GM_xmlhttprequest时语法错误,要么被安全设置阻止。

so for cross site requests should i be using 因此对于跨站点请求,我应该使用

var Request = require("request").Request;
    Request({
      url: "http://10.1.1.1/app/site_search_info.php",
      headers: {
             "Content-Type": "application/x-www-form-urlencoded"
          },
      content: encodeURI("link=" + itemDir[2]),
      onload:function(r)
            {
................ original html formating code here... 
          };
}).post();

Or should I be using somthing like 还是我应该使用类似的东西

var req = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"] 
                    .createInstance(Components.interfaces.nsIXMLHttpRequest); 
req.open('PUT',"http://10.1.1.1/app/site_search_info.php", false); /* synchronous! */ 
req.onload = function (r)
         { 
................ original html formating code here... 
          }; 
req.setRequestHeader('Content-Type', application/x-www-form-urlencoded); 
req.send(encodeURI("link=" + itemDir[2]))

I need to post a items name it ends up encoding "link=pliers" and take the time date result that the php page generates, and run it through the formatting code then append the original web page with that data. 我需要发布一个最终以“ link = pliers”编码的项目名称,并获取php页面生成的时间日期结果,并通过格式代码运行它,然后向原始网页添加该数据。

Since this is my first XHR in the sdk I need to figure out if it is a syntax error on the XHR or if it is using the wrong type of XHR because security is blocking that. 因为这是我在sdk中的第一个XHR,所以我需要弄清楚这是XHR上的语法错误还是使用了错误的XHR类型,因为安全阻止了它。

If both would work then do they have to be put in a specific .js file or called in a specific way? 如果两者都可行,那么是否必须将它们放入特定的.js文件或以特定方式调用?

You should be using the SDK's Request module, the first option. 您应该使用SDK的“请求”模块(第一个选项)。 This code should be in main.js, and in your onComplete handler you should send the data to your content script. 该代码应该在main.js中,并且在onComplete处理程序中,您应该将数据发送到内容脚本。 Here is a working example that does something like this: 这是一个工作的示例,它执行以下操作:

// main.js:

let data = require('self').data;

let pm = require("page-mod").PageMod({
    include: /^https:\/\/twitter.com\/.*\/status\/[\d]+$/,
    contentScriptFile: [data.url('jquery-1.7.1.min.js'), data.url('twitter_json.js')],
    onAttach: function(worker) {

        worker.port.on('tweet-loaded', function() {
            let tweet_id = /status\/([\d]+)$/.exec(worker.tab.url).pop();
            if (tweet_id) {
                let twitter_request = require("request").Request({
                    url: 'https://api.twitter.com/statuses/show/' + tweet_id + '.json',
                    onComplete: function(response) {
                        worker.port.emit('got-tweet-json', response.json);
                    }
                }).get();
            }
        });
    }
});


// content script

$(function() {
    // this setTimeout is a dumb hack, it seems we need to wait a bit longer before
    // modifying the page. I blame new twitter.
    window.setTimeout(function() {
        self.port.on('got-tweet-json', function(data) {
            var s_data = JSON.stringify(data, null, '   ');
            $('ul.actions').after('<pre id="json-dump"></pre>');

            $('ul.actions').append('<li><a id="json-link" href="#">View JSON</a></li>');

            $('#json-link').click(function(ev) {
                $('#json-dump').toggle();
                return false;
            });
            $('#json-dump')
                .html(s_data)
                .hide();
        });

        self.port.emit('tweet-loaded', true);


    }, 200);
});

https://builder.addons.mozilla.org/addon/1040966/latest/ https://builder.addons.mozilla.org/addon/1040966/latest/

Not the window.setTimeout - I think this is necessary in this case because of all the JS that gets executed on load when viewing new twitter. 不是window.setTimeout-在这种情况下,我认为这是必要的,因为在查看新Twitter时,所有JS都会在加载时执行。 I be it varies in necessity between OS and browsers. 我是因为操作系统和浏览器之间的必要性不同而异。 :D :D

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

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