How to use CryptoJs in the Javascript - javascript

I installed crypto-js like so in my Blazor project
PM> npm install crypto-js
then in the index.html, I used it like so
<script>
... OTHER FUNCTIONS
function encryptMessage(message, key) {
const keyOffSet = 10;
const data = CryptoJS.enc.Utf8.parse(message);
const keyLength = 24
}
</script>
How come that I get an error of
Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: CryptoJS is not defined
ReferenceError: CryptoJS is not defined

You need to import CryptoJS by adding CryptoJS CDN. Add the following code before your script tag which contains the logic.
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js" integrity="sha512-E8QSvWZ0eCLGk4km3hxSsNmGWbLtSCSUcewDQPQWZF6pEU8GlT8a5fF32wOl1i8ftdMhssTrF/OhyGWwonTcXA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
Note: If you still get error, check details of the GET request performed in Dev Tool's Network tab, it would probably have to do with CORS or some other network policy.

Related

500 process is not defined ReferenceError: process is not defined

I am getting this problem every time I import a lib or when I use puppeteer and I don't know how to fix it. I am trying to get some data from LinkedIn using https://www.npmjs.com/package/linkedin-client
the code is easy:
import LinkedinClient from 'linkedin-client';
async function getIt() {
const session = supabase.auth.session();
const tok = session?.provider_token;
const token = JSON.stringify(tok);
console.log(token);
const client = new LinkedinClient(token);
const data = await client.fetch('https://www.linkedin.com/in/some-profile/');
console.log(data);
}
at first it gives me this error:Module "util" has been externalized for browser compatibility. Cannot access "util.promisify" in client code
after I install npm i util then it displays the following error:
500 process is not defined ReferenceError: process is not defined
Can you please let me know how to fix it?(I'm using sveltekit)
The library requires to be run on the server. It has be in a server endpoint, it cannot be in a component or a load function.
If this is already the case, this might be an issue with Vite trying to remove server dependencies. There is e.g. a plugin #esbuild-plugins/node-globals-polyfill which polyfills the process variable. It may also be necessary to list packages in resolve.alias in the Vite config, to point to the Node modules.

XML Encryption and Decryption

I want to encrypt and decrypt my xml file.
I read w3c recommendation about this process.
by some searching
I found npm xml-encryption 1.2.0
https://www.npmjs.com/package/xml-encryption
Has anyone tried it before?
I've installed it but it shows me that error
"require is not defined"
Have you guys any other ways to encrypt xml?
my code
<html>
<head>
<script>
var xmlenc = require('xml-encryption');
var options = {
rsa_pub: fs.readFileSync('./public.pem'),
pem: fs.readFileSync('./public.pem'),
encryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#aes256-cbc',
keyEncryptionAlgorithm: 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p',
disallowEncryptionWithInsecureAlgorithm: true,
warnInsecureAlgorithm: true
};
xmlenc.encrypt('content to encrypt', options, function(err, result) {
console.log(result);
})
</script>
</head>
<body>
</body>
</html>
I found the solution here
Client on node: Uncaught ReferenceError: require is not defined
firstly I thought this error is related to the package xml-encryption because I already have installed nodejs

Uncaught TypeError: Module.cwrap is not a function

I need to decode h264 data at browser side for that I am using openh264 library build in web Assembly using emscripten.
I have build it successfully and tried to use it in java script to decode the h264 data. But I am getting one error for following line,
var open_decoder = Module.cwrap('open_decoder', 'number', null);
Error is: Uncaught TypeError: Module.cwrap is not a function
If anyone has has build openh264 with emscripten please help me to figure out issue.
Following steps I have used to build openh264 with emscripten.
$ source emsdk_env.sh
$./emsdk activate latest
cd openh264-js-master
make
Note : The code for openh264 has been downloaded from github( ttyridal) and already has make file with emscripten competent.
-s EXTRA_EXPORTED_RUNTIME_METHODS=["cwrap"]
Include above in command line while compiling your source
emcc source.c -s EXPORTED_FUNCTIONS=['_my_add'] -s EXTRA_EXPORTED_RUNTIME_METHODS=["cwrap"]
Probably you are trying to use Module before the Emscripten runtime has been initialized, so Module.cwrap is undefined.
To make sure the runtime is ready, place your code inside of Module.onRuntimeInitialized, as in the following example:
<!doctype html>
<html>
<body>
<script>
var Module = {
onRuntimeInitialized: function() {
my_add = Module.cwrap('my_add', 'number', ['number', 'number'])
alert('1 + 2 = ' + my_add(1, 2));
},
};
</script>
<script async type="text/javascript" src="index.js"></script>
</body>
</html>
See full example in this github repo

Uncaught Error: Module name "https" has not been loaded yet for context: _. Use require([])

This is the error that I'm getting-
Uncaught Error: Module name "https" has not been loaded yet for context: _. Use require([])
http://requirejs.org/docs/errors.html#notloaded
at makeError (require.js:168)
at Object.localRequire [as require] (require.js:1436)
at requirejs (require.js:1797)
at Dataverse_API_node.js:1
I have an explore.html page which calls the script Dataverse_API_node.js, which is itself dependent on the require.js file. My Dataverse_API_node.js script is supposed to display its output data on the explore.html page. However, I face the above error when I load the explore.html page.
Note that my Dataverse_API_node.js script runs independently without errors and displays the data in the command prompt when run through the Node.js command prompt.
There are the first few lines of the Dataverse_API_node.js script-
const https = require("https");
var parseString = require('xml2js').parseString;
const demo_url = "https://demo.dataverse.org"
const actual_url = "https://demo.dataverse.org/dataverse/hbstest"
const search_uri = "/api/search"
const options = "?q=*&subtree=COSgak"
This is the part of explore.html where I'm calling the Dataverse_API_node.js script and supposed to display the data-
<p>
<script src="Dataverse_API_node.js" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.5/require.js">
document.write(search_for_all("https://demo.dataverse.org", 0, 'file'));
</script>
</p>
I'm new to Javascript and would really appreciate some help with this error. Please don't send me to requirejs.org as it doesn't really help.
Thank you for your time.
Solution found. Just use browserify. Install browserify and then run the following command-
> browserify Dataverse_API_node.js > bundle.js
Browserify goes through all the 'require' in your script and wraps it up in bundle.js and helps to run the node.js modules in the browser.
Just add the following script in your .html file-
<script type=text/javascript src="scripts/bundle.js"></script>

elasticsearch.js: ReferenceError: require is not defined

I'm trying to create a project using elasticsearch.js. I'm following the docs found here.
In the <head> I've loaded:
<script src="js/jquery-1.10.2.js"></script>
<script src="js/elasticsearch.js"></script>
<script src="js/esInit.js"></script>
and in custom.js I have
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
{Host Here}
});
In FB console I see
ReferenceError: require is not defined
var elasticsearch = require('elasticsearch');
I'm reading that require() is not a function of javascript. Very confused.
It looks like you are loading the version that is supposed to be used with Node.js.
There are browser compatible builds available as well: http://www.elasticsearch.org/guide/en/elasticsearch/client/javascript-api/current/browser-builds.html
Yes require is not a default function in javascript. Instead you need to download that js file from "http://requirejs.org/docs/download.html"

Categories