Chrome Extension get local json with jQuery - javascript

I have a json with some data for a Chrome extension I'm making. The extension uses jQuery. I'm trying to read the data in my javascript though it doesn't work. I'm using jQuery.getJSON:
console.log("Start loading sets");
$.getJSON("AllSets.json", function (data) {
console.log("done");
});
I get the "Start loading sets" call, but not "done". I read the getJSON is actually to get a JSON from a server using http, so that could explain why it doesn't work for local jsons. So what should I use to read the included json?
Thanks!

To access a resource in your extension from a content script, first add it to the "web_accessible_resources" section of your manifest, then use chrome.extension.getUrl() to get a link to it. Then you can use an XMLHttpRequest (or your preferred wrapper around it) as usual.

Related

Reading JSON that is written outside of a content script file Chrome Extension

I am trying to read a JSON object located in another js file using a chrome extension content script.
I tried listening for the page loading, and even used timeouts but it didn't work.
Is it possible to somehow access that json file? I am able to get it using the console, but not when using the content script js.
MCVE:
There is one file that is loaded by the website. Let's call it "loader" for now.
Loader defines a JSON object called "Derulo" and it contains this information {"Artist":false}. I am creating a Chrome extension that wants to use content script in order to take the "Derulo" JSON object that the website defined.

Easiest way to load and read a local text file with javascript?

I have a .csv file that I wish to load that contains information that the .HTML page will format itself with. I'm not sure how to do this however,
Here's a simple image of the files: http://i.imgur.com/GHfrgff.png
I have looked into HTML5's FileReader and it seems like it will get the job done but it seems to require usage of input forms. I just want to load the file and be able to access the text inside and manipulate it as I see fit.
This post mentions AJAX, however the thing is that this webpage will only ever be deployed locally, so it's a bit iffy.
How is this usually done?
Since your web page and data file are in the same directory you can use AJAX to read the data file. However I note from the icons in your image that you are using Chrome. By default Chrome prevents just that feature and reports an access violation. To allow the data file to be read you must have invoked Chrome with a command line option --allow-file-access-from-files.
An alternative, which may work for you, is to use drag the file and drop into onto your web page. Refer to your preferred DOM reference for "drag and drop files".
You can totally make an ajax request to a local file, and get its content back.
If you are using jQuery, take a look at the $.get() function that will return the content of your file in a variable. You just to pass the path of your file in parameter, as you would do for querying a "normal" URL.
You cannot make cross domain ajax requests for security purposes. That's the whole point of having apis. However you can make an api out of the $.get request URL.
The solution is to use YQL (Yahoo Query Language) which is a pretty nifty tool for making api calls out of virtually any website. So then you can easily read the contents of the file and use it.
You might want to look at the official documentation and the YQL Console
I also wrote a blog post specifially for using YQL for cross domain ajax requests. Hope it helps
You can try AJAX (if you do not need asynchronous processing set "async" to false. This version below ran in any browser I tried when employed via a local web server (the address contains "localhost") and the text file was indeed in the UTF-8-format. If you want to start the page via the file system (the address starts with "file"), then Chrome (and likely Safari, too, but not Firefox) generates the "Origin null is not allowed by Access-Control-Allow-Origin."-error mentioned above. See the discussion here.
$.ajax({
async: false,
type: "GET",
url: "./testcsv.csv",
dataType: "text",
contentType: "application/x-www-form-urlencoded;charset=UTF-8",
success: function (data) {
//parse the file content here
}
});
The idea to use script-files which contain the settings as variables mentioned by Phrogz might be a viable option in your scenario, though. I was using files in the "Ini"-format to be changed by users.

jquery load to hide content

There is javascript on my webpage, but I need to hide it from my users (I don't want them to be able to see it because it contains some answers to the game.)
So I tried using Jquery .load in order to hide the content (I load the content from an external js file with that call). But it failed to load. So I tried ajax and it failed too.
Maybe the problem comes from the fact that I'm trying to load a file located in my root directory, while the original page is located in "root/public_html/main/pages":
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
url : "../../../secret_code.js",
dataType: "text",
success : function (data) {
$("#ajaxcontent").html(data);
}
});
});
</script>
1) Why can't I load a file from the root directory with ajax or load method?
2) Is there another way around?
PS: I'm putting the file in the root directory so people can't access it directly from their browsers...
1) if the file isn't accessible via web browsers, than it's not accessible via ajax (ajax is part of the web browsers
2) try /secret_code instead of ../../../secret_code.js
What is your system setup? Are you using a CMS?
Even if you add the javascript to the page after page load a user with a tool like firebug can go and view it. I don't think what you are doing is really going to secure it. An alternate solution is that you could minify and obfuscate the javascript that you use in your production environment. This will produce near unreadable but functioning javascript code. There are a number of tools that you can run your code through to minify and obfuscate it. Here is one tool you could use: http://www.refresh-sf.com/yui/
If that isn't enough then maybe you could put the answers to the game on your serverside and pull them via ajax. I don't know your setup so I don't know if that is viable for you.
Navigate to the URL, not the directory. Like
$.ajax({
url : "http://domain.com/js/secret_code.js",
..
Even if you load your content dynamicly, it's quite easy to see content of the file using firebug, fiddler or any kind of proxy. I suggest you to use obfuscator. It will be harder for user to find answer
Take a look at the jQuery.getScript() function, it's designed for loading Javascript files over AJAX and should do what you need.
Try jQuery's $.getScript() method for loading external
Script files, however, you can easily see the contents of the script file using Firebug or the developer toolbar!
Security first
You can't access your root directory with JavaScript because people would read out your database passwords, ftp password aso. if that would be possible.
You can only load files that are accessible directly from browsers, for example, http://www.mydomain.com/secret_code.js
If it can't be accessed directly by the browser, it can't be accessed by the browser via ajax. You can however use .htaccess to prevent users from opening up a js file directly, though that doesn't keep them from looking at it in the google chrome or firebug consoles.
If you want to keep it secret, don't let it get to the browser.

Browser Problems Parsing XML With JQuery

I need to access XML files with JQuery. I tried these two ways but I can't seem to contain them on one browser.
Here:
In Chrome, this works:
var xml = "<music><album>Beethoven</album></music>";
var result = $(xml).find("album").text();
alert(result);
Now I try to use it in conjunction with this code snippet, which works fine in IE:
jQuery.get('fruits.xml', function(data) {
alert(data);
});
The problem is, if I put both these codes together, one of them don't work on the other. So in Chrome I'd be able to access "Beethoven" while not being able to access "fruits.xml" but gives me this error:
XMLHttpRequest cannot load file:///C:/Python32/fruits.xml. Origin null is not allowed by Access-Control-Allow-Origin.
In IE on the other hand, I could access the whole content of fruits.xml and save it into a variable but the code for which I need to access XML attributes which works in Chrome doesn't work in IE.
As you can see, I want to get the contents of the xml using the 2nd snippet of code, while I will access the contents of the xml using the 1st snippet of code. Is there another way to access XML with Javascript? Can you help me with what's wrong with my codes?
Help?
The problem causing the error message is that you're sending an XHRRequest (The A in *A*JAX) to a file:// URL. For security reasons, this is being disabled by modern browsers. Instead, set up a webserver und it to access your page, via http://localhost/.. instead of file://C:/....
For the second part, make sure that you test loading fruits.xml first. Chances are there is an error in the XML stored in this file, or its structure is not what you expect. You can debug JavaScript in IE by pressing F12, going to the Scripting tab, and clicking Start Debugger. That way, you'll get a better error description than "doesn't work".
this may be a bug associated with chrome
loading local files in chrome bug/security feature
Problems with jQuery getJSON using local files in Chrome
Accessing relative URL's via "ajax" from "file://" content
as an advice dont wrap your xml in jquery and parse this way it may vary from browser to browser use instead .parseXML

Ajax Parsing Local XML

I'm building a local html file to show dynamically some data from an XML file and by using Chrome's Inspector I figured my XML file is not being parsed because it is "not hosted on a webserver"
XMLHttpRequest cannot load data.xml. Cross origin requests are only supported for HTTP.
I know that there are a few flags I could pass to Chrome/web browser to workaround this limitation but I'm looking into some alternative method. I will probably have to distribute this files to a few people and teaching them how to pass flags to the browser is not an option for me. Hosting them on a web server is not an option either.
Thanks a lot in advance.
No ghost unless you set up a local server or host the XML file locally. Ajax has to follow the same origin policy.
If you're willing to use a library you can use jQuery's AJAX method to perform a cross-domain request (i'm not entirely certain that jQuery will support what you're trying to do). JSONP works, but you have XML data...
You could try loading it in a script tag anyway and see if you can get the innerHTML value without breaking the script; once you're done getting the text from it, remove the script from the page. You may be able to get at the data before the browser tries to parse the script by attaching onload and onreadystatechange events to the script element.
var s = document.createElement('script');
s.src = '/path/to/file.xml';
s.onload = s.onreadystatechange = getData;
function getData(e)
{
//get the text out of the script element
//remove the event handler from the script element
//remove the script from the page
}
document.getElementsByTagName('body')[0].appendChild(s);
I didn't test it, but it might work.
How about setting up a local webserver? XAMPP should be easy to install even for novice. Just tell them to put your files in the htdocs folder, run xampp and start the apache server.
Unless there's a compelling reason to have a separate html and xml file, you could just put the data directly in the html file.
I'm afraid if chrome only provides options that you don't like to apply, your application and chrome will not come together. Access via iframe & object does'nt work too, load()-method of createDocument is not supported by chrome(if it does I guess you got the same error).
Whatever you try will be a sideway to pass chrome's restrictions, what cannot be good, because I think they have good reasons to setup these restrictions.

Categories

Resources