Greasemonkey help with remote xml using jquery

ertaboy356b

Old School Gamer
Reaction score
86
Hi, I recently used greasemonkey (actually, it's just today). I have no experience about its API whatsoever. I have found a script that lets you use jquery for greasemonkey. I also found a script that let you use the gm_xmlhttprequest with the jquery wrapper ($.post, $.get).

Now I have a problem with greasemonkey requesting xml file from a remote host. I created a script data.xml which retrieves data from a server and returns it as an xml file. Greasemonkey can't seem to request this file from this server, probably because of security reasons. I've tried requesting it from localhost but failed.

Any workaround regarding this?? The script can be read below btw.

HTML:
// ==UserScript==
// @name          Blogger Poster
// @namespace     http://syncjournal.blogspot.com
// @description	  Personal Blogger Poster
// @author            Lynxx
// @homepage       http://syncjournal.blogspot.com
// @include       http://www.blogger.com/post-create.g?blogID=*
// ==/UserScript==

var $;

// Add jQuery
(function(){
	if (typeof unsafeWindow.jQuery == 'undefined') {
		var GM_Head = document.getElementsByTagName('head')[0] || document.documentElement,
			GM_JQ = document.createElement('script');

		GM_JQ.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js';
		GM_JQ.type = 'text/javascript';
		GM_JQ.async = true;

		GM_Head.insertBefore(GM_JQ, GM_Head.firstChild);
	}
	GM_wait();
})();

// Check if jQuery's loaded
function GM_wait() {
	if (typeof unsafeWindow.jQuery == 'undefined') {
		window.setTimeout(GM_wait, 100);
	} else {
		$ = unsafeWindow.jQuery.noConflict(true);
		letsJQuery();
	}
}

// Wrapper for GM_xmlhttpRequest
function GM_XHR() {
    this.type = null;
    this.url = null;
    this.async = null;
    this.username = null;
    this.password = null;
    this.status = null;
    this.headers = {};
    this.readyState = null;

    this.open = function(type, url, async, username, password) {
        this.type = type ? type : null;
        this.url = url ? url : null;
        this.async = async ? async : null;
        this.username = username ? username : null;
        this.password = password ? password : null;
        this.readyState = 1;
    };

    this.setRequestHeader = function(name, value) {
        this.headers[name] = value;
    };

    this.abort = function() {
        this.readyState = 0;
    };

    this.getResponseHeader = function(name) {
        return this.headers[name];
    };

    this.send = function(data) {
        this.data = data;
        var that = this;
        GM_xmlhttpRequest({
            method: this.type,
            url: this.url,
            headers: this.headers,
            data: this.data,
            onload: function(rsp) {
                // Populate wrapper object with returned data
                for (k in rsp) {
                    that[k] = rsp[k];
                }
            },
            onerror: function(rsp) {
                for (k in rsp) {
                    that[k] = rsp[k];
                }
            },
            onreadystatechange: function(rsp) {
                for (k in rsp) {
                    that[k] = rsp[k];
                }
            }
        });
    };
};

// All your GM code must be inside this function
function letsJQuery() {
	//alert($); // check if the dollar (jquery) function works
	//alert($().jquery); // check jQuery version
	$.ajaxSetup({
		xhr: function(){return new GM_XHR;}
	});
	pospost = '<div id="gmform"><input class="gmtext" id="gmurl" type="text" value="URL" /><input class="gmtext" id="gmtitle" type="text" value="TITLE" /><input class="gmtext" id="gmcreate" type="button" value="CreatePost" /></div>';
	$("body").append("<style>#gmform { background-color: #eee;border: 2px solid #222;width: 500px;height: 200px;position: absolute;top: 20px;left: 25%;padding: 10px;z-index: 5000;}.gmtext {background-color: #efe;}</style>");
	$("body").append(pospost);
	$("#gmcreate").click(function() {
		gmurl = $("#gmurl").val();
		gmtitle = $("#gmtitle").val();
		alert("gana1");
		$.get("http://syncjournal.net46.net/parser/data.xml",{url: gmurl, title: gmtitle},function(xml) {
			gmpost = $("post",xml).text();
			gmtitle = $("title",xml).text();
			alert(gmpost);
			$("#postingTitleField").val(gmtitle);
			$("#postingHtmlBox").val(gmpost);
		});
	});
	
}
 

Magentix

if (OP.statement == false) postCount++;
Reaction score
107
XHRs are subject to the Same Origin-policy.
Unless you enabled Cross-Origin Resource Sharing on the remote host, it's only natural you're not receiving any data.
 

ertaboy356b

Old School Gamer
Reaction score
86
Is it possible to use localhost with it? I don't know how to make XHR work in GreaseMonkey.. I've seen people done it in the internet..
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top