How to get content of javascript files as variable - javascript

I have to develop a chrome extension for my thesis which does following analysis:
This extension should analyse the javascript files (.js) and webssembly files (.wasm) of visited webpages. For example i have to check if a javascript file contains a special string X. In somepages included files are on visited domain in somepages on another domain. Via security reasons maybe it would be diffucult to get content of these files. Is it possible to get content of these files as a variable?

Get file contents with jquery
jQuery.get('http://localhost/filename.ext', function(data) {
var jsFile = data;
});

Related

Javascript - read all filenames and output to html with hyperlink

I have a limited hosting server. I want to read the filetree (all files and folders) and create a hyperlink to them on a basic html page.
For clarity, I'm using Keybase, am publicly sharing files, but want to list them on an index.html page, not use their site's "filetree"
https://keybase.pub/example_user (keybase filetree)
https://example_user.keybase.pub (the index.html file)
The html file is in the root directory and I want to display all the (pdf) files in /subdir (and their sub-directories)
This isn't a "real" webserver. I'm looking for something easy and simple like a FOR loop on load within html
Thanks.
What you are trying to do is known as "screen scraping". If you do some googling on the keywords "javascript screen scraping" you will find lots of information and examples.
Basically, You fire off an AJAX request to retrieve the content of a page, parse that content to obtain the data your looking for, and then display that data in your page.

How to find html files running from server or not using javascript?

I'm new to javascript might be this question looks silly. How to check whether the html files are loading from local file system or server using javascript?
For example when I open the html files from my local system (/home/user/1.html) browser shows in url
file:///home/user/1.html
But if i load 1.html file into my local server then, if i access that file browser shows in url like below
http://localhost/GUI/1.html
I want to find whether files are loading from my server or local file system using java script.
Is there any way to find this using java script method.
You can use the window.location object to do that.
use window.location.protocol property.
The following line of code will return true if the file is being served from the file system.
window.location.protocol == "file:";
It basically checks if the protocol being used is file or not.

How can i read local file from D drive in javascript

My requirement is to read the json file which contains some data and store in some other .js file.
I got task to read local file from local disk in Javascript , i have used file path like - D:\json\analytics.json.
(document).ready(){
($).getData("D:\json\analytics.json");
}
when i see in firebug it takes other url.
How I can do it, is it possible to read file from javascript.
I don't know javascript , i have seen some answer but i am not able to understand .
Need Solution , how I can achieve it. is there any other way to read file on jsp without using scriptlet . From server side , can send it on the jsp page.
I think Jaronmanda's answer won't work cause it will hit cross origin issue, see "Cross origin requests are only supported for HTTP." error when loading a local file.
As the page suggested, in general you need to serve that json file from a web service (same domain, or allow your domain to access), but it depends on what you really need to do. If you can control where that json file is stored, the easier way is to put that in a subdirectory of your html file, and do:
$(document).ready(function () {
$.get('<directory>/analytics.json', function (data) {
// Do your stuff
});
});

External URL linking

I have the following code:
<script>
var fileContent;
$.ajax({
url : "text.txt",
dataType: "text",
...
It loads the text from a .txt file in order to obtain the data. If the text.txt is on the same path as the html code, it loads the data. However, if I type for example (placing the file in a different folder):
url: "../../../files/text.txt"
It does not allow me to obtain the file. Any ideas of how to do it or how to implement it without changing the code in a significant way? Thanks!
There are three possible causes of this:
You are using HTTP
You are using HTTP and the path you are trying to access is not exposed by your web server. (You cannot access files above the directory root by default).
You need to give the file a URL on your web server and request that URL.
You are using local files
Different browsers have different security restrictions for Ajax on local files.
You can get this problem if the file is in a directory above the HTML document (some browsers only allow you to access files in the same or a lower directory).
You can resolve this by using HTTP. Ajax in general works very poorly without HTTP.
You simply have the URL wrong
Correct the URL.

Getting contents of a local file in Javascript

I've currently got a folder with 2 files in it. The first file is output.log which is the text that I want to somehow load into the javascript code. The second file is view.html which contains the javascript. The html file will only be opened on the local users machine and not on a website.
I've tried to embed output.log as an iframe and read the contents, but that has not worked.
The browser prevents a script from manipulating data in an iframe if the origin of the data contained in the iframe is different from the containing web page. This is to prevent cross-site scripting attacks.
You could use the jQuery get() function and then output the returned content to a named <div> element
$(document).ready(function() {
$.get('path/to/output.log',
function(data){
$('.somediv').html(data);
}
);
});

Categories

Resources