Javascript .pac file - javascript

Is there a way to get a browser to read a .pac file without having the user manually point the browser's automatic config file at the URL?

Are you talking about Proxy Autoconfiguration?
Basically you have to configure a host name such as wpad.example.com and put the file there named as wpad.dat
See here: http://en.wikipedia.org/wiki/Web_Proxy_Autodiscovery_Protocol

If you are using Chrome then you can use the chrome switch --proxy-pac-url
For example:
Open cmd.exe as admin
Make sure to first navigate to the path of Chrome (1st line) in cmd
Notice also that I used another chrome switch which is unsafe-pac-url this will allow your pac file to work for HTTPS requests, because otherwise chrome will only apply our PAC file to HTTP requests only.

Related

Reaching and Pulling Environment Variables in TamperMonkey

Currently my Office is running a AHK script to pull environment variables. These Env Variables are then used as a specific outputted data when closing tickets as my Office has a ticket closing environment. This works for the time being however I am looking into automating this process and starting off just trying to auto close the tickets when a specific key is pressed. I have been able to perform this task but I have to basically have static variables in the TamperMonkey script for each user. Everyone using this ticket site has the specific environment variables already due to the AHK script and want to try and implement this into the Tampermonkey script without having to change the site completely.
I have locally hosted the site and used Node to do this and I am successful in doing this but it does not work on the Tampermonkey route. I have been using process.env.ENV_VARIABLE on the node side but I am trying to refrain from completely implementing this on the site itself. I have added some basic variable examples in a Autohotkey Script already being used.
GetGreeting() {
global greeting
return greeting
}
GetSalutation() {
global salutation
return salutation
}
GetUserName() {
Envget, e_Ticketname, Ticketuser
return e_Ticketname
}
When a specific Key is pressed it should write the specific message and include said specific Env Variables. Currently I don't think I have it where Tampermonkey can actually understand the Environment Variables as it keeps giving a undefined error. Any Ideas.
So upon further investigation it does not appear to be a way to interact with the OS inside the browser. I will be looking into another way to do what I am looking for. Thank you!
You are able to access and run local files as JavaScript code in Tampermonkey using \\ #require
So if you're able to have a local file with the content in this format:
variables = {
var1: "hello there"
}
Then in the script, add this line and add the path to the file.
// #require file://Path\to\file
Since all the file has is an assignment to a variable, then you can access that in the script
console.log(variables.var1) // logs "hello there"
For this you need to give the extension access to file URLs:
go to chrome://extensions
Tampermonkey > Details
Allow access to file URLs
You'll still need a way to generate the file in that format in the user machine though, either manually, or some code running locally.
A way to generate the file, could be using Node locally, but if you're running that locally, at that point another way to get local data is to serve it using a simple http server (like Node server), then you could make a request from Tampermonkey using fetch or GM_xmlhttpRequest
As a side note, a hack I use to run code locally triggered with Tampermonkey, is to use the localexplorer extension. The extension allows you to open files and folder from the browser using "localexplorer://" urls, so then with javascript you can do window.open(local_url) and it will run or open that file/folder. The file can also be a .bat file, and you can run anything from it (including Node code).
There are some security considerations for using this though if you're worried other websites might be able to open files in your system. but the extension prompts you every time you try to open something with localexplorer
If you're still interested on this, a way I use for it to work without the prompt with less risk is this:
The prompt also lets you click the checkbox of Always open links of this type in the associated app for each domain. So then what you can do, is have a specific domain you choose for this, to always use that domain to open localexplorer links, and use a format of your choosing, like secretdomain.com/?C:\\path\\to\\file, and grant access to always open the links on that domain. Then use Tampermonkey to run some code on that domain so that when it detects that specific url format, to redirect the page to a localexplorer url, like this
location.href.replace(/htt.*:\/\/secretdomain.com\/\?/,'localexplorer:')

How to open a file in filesystem: from a link

I have created a file using the FileSystem API. I can't open it from a simple link. However, I can open the page placing the URL manually in the browser (filesystem:http://localhost:8100/temporary/log.csv)
I'm testing only on google chrome for now.
window.open("filesystem:http://localhost:8100/temporary/log.csv")
I expect it to open in a new window.
UPDATE:
This issue was fixed by using window.URL.createObjectURL()
so the link will be something like blob:http://localhost:8100/7aa5685c-ca4f-485a-8bf8-d1c95e6257ab which works
If you know where in your filesystem is the root folder of your webserver (for example: d:\apache\www_root\temporary\) then copy your csv file to there.
Once its done, you should be able to open it from your Javascript by using:
window.open("http://localhost:8100/temporary/log.csv")
Same issue.
Using FileSystem API since the product is Chrome only.
How did you turn the FileEntry into the objectURL?
Figured it out
// turn fileEntry to file
fileEntry.file((f) => {
console.log(window.URL.createObjectURL(f));
});

External javascript file not executing

The problem is that the following external javascript file is not executing:
<script src="//www.google-analytics.com/cx/api.js?experiment=YOUR_EXPERIMENT_ID"></script>
When I access the URL directly it downloads the file as f.txt
And when I copy/paste the content from the file then it's fully functional js and working as it should: creating cxApi object.
Got the snippet from: https://developers.google.com/analytics/solutions/experiments-client-side
Presumably you are testing using a local file.
i.e. the URL in your browser's address bar is something like file:///Users/you/Desktop/test.html
The relative URL //www.google-analytics.com/cx/api.js?experiment=YOUR_EXPERIMENT_ID then resolves to file://www.google-analytics.com/cx/api.js?experiment=YOUR_EXPERIMENT_ID, which does not exist.
You may see an error in the Console of your browser's developer tools. You should see one in the Network tab.
Test using a web server. Access your pages over HTTP
Then you will have something like http://localhost/test.html, and the URL will resolve to http://www.google-analytics.com/cx/api.js?experiment=YOUR_EXPERIMENT_ID, which does exist.

JavaScript googlemaps read coordinates from file

I want to display some markers using googlemaps. The information (coordinates) are stored in a local *.csv file (wich I want to use a "ressource-file").
How can I read this *.csv file? If I use "jQuery.get('myFile.csv', function (data) {..." it dosn't work.
The error message is: Cross origin requests are only supported for protocol schemes
Do I hava to make a file selection to read the file? Is there no other way?
Thanks
Is it possible that you are trying to load data from a file and not a running server(for example by double-clicking the .html from your file manager)?
If your are on the file:// protocol (which you can see in your url) this will not work. You could try changing to the development directory and runnig python3 -m http.server which will start a small development server. You can than change to http://localhost:8000 and see if it works.

Starting download of an url shortcut file?

I have created a shortcut file by going to Desktop -> new shortcut and entered link.
Now I have uploaded this shortcut file (*.url) on the
root on my server as shortcut.url
When i directly access mysite.com/shortcut.url, it does not start the download of it but instead show the content of the .url file.
Now on my page where i link to mysite.com/shortcut.url, I have tried the following methods:
How to start automatic download of a file in Internet Explorer?
But noone of them seems to work in Chrome (I though that if it their answers work in IE then Chrome it would too).
How is it possible to start downloading of this type of file, on click?
Generally speaking, a browser will fetch a resource in download mode (rather than displaying directly) if the content type is one that it cannot handle, and no plugins can handle. The easiest way for that to be the case is by using the content type for generic binary files:
Content-Type: application/octet-stream
Basically, configure your web server to use this content type (often called a MIME type) for .url files. How you do that depends on what server you're using.

Categories

Resources