I am building an add-on that has multiple .js files that are associated with it many of which need the access to the require() function but when i use the require function in them i get the error that require is not defined.used importScripts() to include the require.js file but the import scrips also generates error.
importScripts('resource://gre/modules/workers/require.js');
Also Used
self.importScripts('resource://gre/modules/workers/require.js');
The error generated is
JPM undefined Message: ReferenceError: importScripts is not defined
And
JPM undefined Message: ReferenceError: importScripts is not defined
Need help to include multiple files that can have the access to the require() or importScripts() function.
Looks like you're using the add-on SDK.
You can't use privileged code with all JS files, that includes require(). You can only use privileged code from your main.js script. Then use a content script/worker to communicate between the main script and the other script.
What about:
let loader = Cc["#mozilla.org/moz/jssubscript-loader;1"]
.getService(Ci.mozIJSSubScriptLoader);
loader.loadSubScript("chrome://marionette/content/simpletest.js");
Here's a nice example on using webworkers in addons, doesnt mention the importScripts function though :-(
how to use webworkers in Mozilla addons
you could try adding them as normal using the importScripts function in your worker script but give full web urls to the worker scripts, FF doesn't seem to complain.
Related
I am trying to create a local browser-based application.
I need to have access to some dlls so I used a module called ffi-napi:
Example (what I need to use is not specifically the libm module, just simplifying the example):
var ffi = require('ffi-napi')
var libm = ffi.Library('libm', {
'ceil': ['double', ['double']]
})
libm.ceil(1.5)
This works fine when on I am running on Electron. But when I compile it for use with the browser, I am getting this error message (which is being thrown by Webpack/node-gyp-build:
Error in mounted hook: "ReferenceError: require is not defined"
I understand that require is not native on the browser mode, but the framework I am using, I believe, already has browserify integrated (I am using Quasar framework). I think so because the other local modules I can use require() on the browser just fine.
Is there any way I can use ffi-napi on the browser?
Help!
I installed the color-convert library via npm but the browser shows me an error message
Uncaught ReferenceError: require is not defined home.js:134
at HTMLButtonElement.<anonymous> (home.js:134)
at HTMLButtonElement.dispatch (jquery-3.4.0.js:5233)
at HTMLButtonElement.elemData.handle (jquery-3.4.0.js:5040)
JS
var convert = require('color-convert'); // this is line 134
alert(convert.hex.lab('DEADBF'));
I think there is a problem with paths?
require() isn't a function provided by your browser, and is more of a sign that this source code is a common JS module.
In order to use a common JS module, you first need to run your source through a program that bundles the source, sorta replacing each require('other_module') with the source of the other module, producing a single Javascript source file which can included in your frontend HTML.
Two examples of bundlers are browserify and webpack.
I am switching my desktop app from electron to nw.js because of source code security features.
I am requiring the module jquery-confirm in my nodejs file, I get this error:
Uncaught Error: jquery-confirm requires jQuery
I fix this error by:
var $ = require('jquery');
window.jQuery = $;
var jconfirm = require('jquery-confirm');
and importing the js file in index.html like this:
<script>require('./bot.js')</script>
Becuase I get the same jquery error if I require the js file like:
<script src="bot.js"></script>
When I fix the error like above and I launch the app with nw.js, it immediately crashes giving this error:
TypeError: $(...).button is not a function
First question:
I am assuming there is something wrong with jquery. When I import it in index.html, it doesnt work at all. However, I am still running into issues after requiring it in the js file. How can I fix this?
Second question:
Why dont I get the Uncaught Error: jquery-confirm requires jQuery error if I import my js file using 'require' instead of ?
This is an issue of javascript context in Node Webkit. We must ensure all the javascript required inside the browser (the window context) is brought into that context by using the src attribute of a script tag.
Node Webkit places JavaScript from require() statements in the nodejs global context - and makes it unavailable inside the browser.
So we see errors such as $ is not defined or $(...).button() is not a function if files are in opposite contexts.
I have webpage that needs to run some computation on start up. I want to keep this computation on the server side so the client cannot access the source code. I discovered pico, a module that is supposed to be "a bridge between server-side python and client side JavaScript".
I have a simply test.py:
import pico
def hello():
return "Hello World"
My JavaScript is also simple:
pico.load("../../../test.py");
pico.main = function() {
var displayMessage = function(message){
console.log("hello2");
console.log(message);
}
test.hello(displayMessage);
}
"../../../test.py" is the relative location of the python script to the pico folder
I then run "python -m pico.server" on the command line. When I go to my web page, open inspector, and go to the console I get the error: "Uncaught SyntaxError: Unexpected token i". 'i' is presumably from the first line import. Note that this same error happens if I don't run the pico.server command.
Any help would be great, as well as suggestions for alternative methods of doing this serverside vs clientside.
I may have an answer for you, however I have not been able to replicate the same error.
pico.load does not seem to work when file extensions are included in the argument, this is due to the function being designed to load sub-modules directly (i.e. module.sub_module) as in the pico API:
pico.load(module, [callback])
Load the Python module named module. The module will be available as a global >variable of the same name.
Submodules may be loaded by using dotted notation e.g. module.sub_module
To make sure I included ".py" file extension on the pico test page I have been working on and it failed to load the module, so this may be a problem if you are using the file extension.
Another possible issue was mentioned in a comment by holderweb. In the first pico example HTML the file client.js is included in an external <script> tag, this includes the functionality required to use pico. So you must have something similar to the following tag in your index.html head section:
<script type="text/javascript" src="/pico/client.js"></script>
For more insight I would be interested in seeing what/if the server logs at command line when the error occurs, and also the contents of your index.html page. Hope this helped!
I have SDK 1.13 and I want to use pageload API to give the alert message when html form is loaded in firefox browser. but I'm getting an error on console: require is not defined.
I have linked cfx file of add on SDK to file system directory:
ln -s PATH_TO_SDK/bin/cfx ~/bin/cfx
Still, I am not able to solve this error. Here is my code (Included in XUL file):
var pageMod = require("sdk/page-mod");
pageMod.PageMod({
include: "*.html",
contentScript: 'window.alert("Page matches ruleset");'
});
I assume you've installed the SDK and have run bin\activate within your extension before trying cfx run, right?
You can't run this stuff from an XUL file, which is why require... wont work. All of this needs to be in a main.js (in the lib folder). You'll need to communicate via the content script that you'll write (in the data folder). When the html loads (I'd add a window.listener or something from the content script) you'll use port.emit("loaded") or something similar and then you'll have to listen in the main.js with something like addon.port("loaded",somefunction). There's a lot of good documentation on this!
XUL files is quite an opposite of SDK modules. SDK and XUL Comparison.