I came across a situation today where I needed a single Javascript file to be accessible through HTTP. The actual program was running with NodeJS and was outside of the /home/ directory.
The reason I was trying to access the javascript file was it has a list of functions that I use often, and wanted to utilize them with PhantomJS (essentially injecting it into a different website).
I was able to get it to work by moving the entire node into an accessible folder and having htaccess deny access to everything except 1 file, but I am curious if anyone would know:
Would it be possible to take an entire object (functions and preset values) and output them as a string (same as if you navigated to a file.js page). This way I could create an HTTP page, send the proper headers and essentially create an accessible JS page.
I looked around but couldn't find any information on actually outputting a object verbatim.
Check this answer from user Houshalter to the question Converting an object to a string.
All answers provide different examples how to convert object without function to string. But he explains how to do that with object which contains also functions.
Related
I'm experimenting with ways to pre-compile a large number of JS functions using node.js vm.script, so that I can call the functions multiple times without the overhead of recompiling each time. Note that I can't just include the functions in a module, because they are architecturally completely separate from the "core" code. That works fine--each compiled script is stored in an object and can be run as-needed.
What I'm trying to figure out is how to use this same model with multiple fork'ed child processes, so any child process can be used to run the compiled script. The question is how to serialize it, so I can pass it to the child. And more importantly, is there anything inherent in the script variable that would "tie" it to the process it was created in?
I'm afraid is not possible. The first hint is that the Script object doesn't have any method to retrieve the compiled code. Looking further inside the source code of node here and here, it seems that the compiled object is stored inside a native C object. Therefore there's no means to serialize it from JS.
In my extension, I need to access some data stored by page JavaScript.
However, I also need to make sure the page cannot trick my extension into running arbitrary code. The data I need should be stored in plain old value properties (if I understand what they are) on a JS object bound to the page's window, but just in case, I want to be sure not to call any malicious code if it's present.
Is there a way to safely access this data from a content script, treating the JS object (and its children) as a simple dictionary?
I've found two pages that might offer some help:
This page describes a method of getting a reference to the page's window object, but it is unsafe to do so.
This page appears to show that Xray vision provides safe access to the value properties of an object, but does not seem to explain how to get Xray vision for a page-script object, at least from a content-script rather than actual chrome code.
Is there some way to combine these two to allow safe access to the data?
Say you have an object formatted something like the following in one script (javascript)-
var person = {
firstName:"John",
lastName:"Doe",
age:50,
eyeColor:"blue"
};
Is there an intuitive way to "send" this object to another (javascript) script? Basically, i'm trying to communicate data within 2 scripts on a webpage and attempting to avoid having one big script, which would eliminate this problem.
I've already messed around with simply creating a div with a bunch of inner div's which hold the information from the object (All it's holding is text/numbers) but I feel like there has to be a better way of doing this in either javascript or jQuery.
Javascript files are not nodes on a network or system components. They do not "send" or "receive" anything. They are just bits of text that don't do anything by themselves at all.
Rather, your browser loads Javascript files, interprets them, and executes them. They are all loaded into a single, common sandbox and they all share the global namespace. The idea that data is sent from one script to another is simply a misunderstanding of how this all works. If you ask developers how to send data between scripts, they are going to have trouble answering, because the question is more or less gibberish. You have to post your code and be specific about the problem you are having.
If you are receiving an error like "ReferenceError: X is not defined" it is probably because X does not exist in the scope of the function that is throwing the error. For example, if you declared X with the var keyword within a function block and then attempted to access X from another function. If you wish to access a local variable from another function, you will have to pass it as a function argument or parameter.
I can provide a much more precise answer if you post your code.
Quote from Defining JavaScript Resources In QML:
Some JavaScript files act more like libraries - they provide a set of helper functions that take input and compute output, but never manipulate QML component instances directly.
What if I want a JS file that both:
manipulates QML component instances (that are passed to it as arguments)
doesn't get a copy of its code and data stored for every QML component instance that imports it?
I could get the "no data stored on every instance" part by, well, not putting global variables in the JS file. But, for some strange reason, a copy of the "code" part seems to be stored for every instance as well. I don't get why this is but I want to know whether I can circumvent it, and at what cost.
I think that the line you quoted from the documentation is incorrect, or at least very poorly worded; you can still have a JS file with .pragma library in it and manipulate QML objects that are passed in as arguments to its functions. The sentence was probably referring to the previous section.
To share data across qml files, consider using a qml Singleton.
For data sharing purpose, I would not suggest using .pragma library (#Mitch) for following reasons.
.pragma library js provides limited functionality in qml object manipulation. While simple qml object manipulation (like property reading/writing) could be done with a .pragma library js, it does NOT allow creating/deleting qml objects (as you can in regular non-library js). It will suck when your application becomes dynamic.
.pragma library creating only one instance is merely an optimization in Qt implementation. It's never guaranteed that Qt creates exactly one instance, nor that your data would actually be shared.
Well, .pragma library is not designed to do data sharing work from the very beginning. Just, don't try to do it this way.
I have two javascript files in one folder.I want to pass a variable one javascript file to another.what procedure should I use?
window.postMessage is used for cross document messages. Use those messages to share data.
Question is bit confusing,
If the 2 javascript files are in 2 different pages you can use a query string to pass the values.
If both of them are in a single page the value can be simply passed using a variable. Simply define the variable int he first called script file or in the page. It can be accessed from any other javascript in the page defined below the first script.