Access window object from a different js file [duplicate] - javascript

I have an ember application that I create like this:
window.App = Ember.Application.create({});
I want to do some background processing on a web worker.
How can I get access to the window object or some other global object in the separate web worker thread?

Short answer. You can't.
The only resources available to web workers are that which they load from JavaScript files using importScripts() or anything that is passed to them via postMessage().
You can however now pass Objects to them. They are serialized and de-serialized to JSON automatically.
Also, there is no access to local storage from the Worker.

Related

Injecting secret api key

how does one inject an api key into an application?
I have a secret key that I need to use, say AWS credentials
they are stored in a local file that I don't push to github
how would I inject them into the app when it loads in production so that it uses the right key?
Use environment variables, there are a variety of ways you can set them. Such as directly in terminal or in a .env file that gets loaded with a library. Or your deployment solution might have a way to set them.
Then when you need them in your code use
process.env.VAR_NAME

Wrapper for dynamically cache HTTP URLs using service worker?

I have an app that runs service worker. I'm using sw-toolbox library for dynamic caching of URLs, but I want to create wrapper over sw-toolbox that provides getters and setters to my application for URL caching.
As service worker runs in different thread and my application running in main thread, so just wondering how to create a wrapper in Javascript through which my application can communicate with service worker and cache resources on-demand?
So far the Cache api is available from content, that means you can use it directly from your Javascript code, no need to run in the Service Worker.
Check this thread to find the resolution: https://github.com/slightlyoff/ServiceWorker/issues/698 You will be able to use caches from the window object.
Meaning you have all range of Cache methods to play with the content: https://developer.mozilla.org/en-US/docs/Web/API/Cache
Just another reminder, from Chrome 46, you will be able to store stuff just in secured origins.
From the Documentation, Cache API/interface is exposed to windowed scopes as well as workers.
You don't have to use it in conjunction with service workers, even
thought it is defined in the service worker spec.
It depends how your worker caches data, it it just uses the standard "Cache" API than you can just query cache object which is attached to global scope.
In this particular case Cache.match() is your friend.

Pass data from chrome extension to .net app

I'm trying to send data from my chrome extension to my .net application using ajax. I'm using background script to send data. Currently i'm unable to get data at my server. I guess there's issue in setting up manifest for chrome. However, how can i post data from chrome extension?
Suggest any other alternatives if possible.
Thank you.
You can send data to the server using XHR, or use jQuery.ajax() if you prefer. The end point will be the web service you have defined on the server. Check out this example, which uses jQuery for it.
For posting data, you pass all the data you want from the client in JSON format. You can use JSON.stringify() to convert your JavasScript object to a JSON string. If your object matches an entity structure on the server, it should automatically populate it, allowing you to specify that entity as the parameter of the web method. Otherwise, you can accept an object parameter and extract the data from that.
In a Chrome extension, make sure you have the correct cross-origin permissions.
There is a specific mechanism in Chrome for communication with local applications: Native Messaging.
There is an important limitation to keep in mind: Chrome cannot talk to an already-existing process; it can only start a new one and talk over STDIO.
So you may need to have a "proxy" process that Chrome can start, which will connect (somehow, but no longer restricted in methods) to your main app and relay data.

Using deathbycaptcha with phantomjs without interrupting sessions

Considering that PhantomJS isn't exactly node.js (so modules like deathbycaptcha2) are out as they use native requests, is it possible to simply open another instance of webpage and use it to send POST requests to the captcha API without affecting the other page instance?
Will this new page.open() retain cookies collected by the first page?
Will this new page.open() retain cookies collected by the first page?
Yes, there exists only one CookieJar for each PhantomJS process. So every page that you create shares the same cookies. Think of those page instances as windows or tabs in a conventional browser.
[I]s it possible to simply open another instance of webpage and use it to send POST requests to the captcha API without affecting the other page instance?
That's not so easy, since the cookies are shared. If you don't access the same pages you can safely create a second instance. If you want to access the same page in the second instance then you can spin up a second PhantomJS process through the child_process module (e.g. with execFile).
Considering that PhantomJS isn't exactly node.js [...]
True, but there are several bridges between PhantomJS and node.js such as phantom, node-phantom, nightmare, etc. You can use them to interface with PhantomJS and additionally call node modules that you want.

Web workers and accessing object attached to the window object

I have an ember application that I create like this:
window.App = Ember.Application.create({});
I want to do some background processing on a web worker.
How can I get access to the window object or some other global object in the separate web worker thread?
Short answer. You can't.
The only resources available to web workers are that which they load from JavaScript files using importScripts() or anything that is passed to them via postMessage().
You can however now pass Objects to them. They are serialized and de-serialized to JSON automatically.
Also, there is no access to local storage from the Worker.

Categories