Comments (11)
Hello @irwansetiawan,
If you click the first link in the error message you will see the reasons as to why the error may occur.
Let's first take a look at the very first sentence in the RequireJS documentation for this error:
If you manually code a script tag in HTML to load a script with an anonymous define() call, this error can occur.
This means that if you include a <script src="..."></script>
tag in your code that loads a Javascript file with a call to define()
that is providing a name for the module, a common thing to do in RequireJS projects, then this error may be thrown.
js-sha256 does have a call to define an anonymous module, but if we take a look at the suggestions in the documentation on how to avoid this error:
Be sure to load all scripts that call define() via the RequireJS API. Do not manually code script tags in HTML to load scripts that have define() calls in them.
You will then understand that it is not really a fault in js-sha256. I would suggest that you take a look at exactly how you load the file, and see if you can load it using the RequireJS API, rather than using a script tag.
For more information on using the RequireJS API for loading files in the browser, take a look here: http://requirejs.org/docs/api.html#jsfiles
from js-sha256.
thanks for @hakash 's explain. The correct way to use RequireJS should look like this:
require(['path/to/sha256.js'], function (sha256) {
sha256('...');
});
from js-sha256.
Hi @hakash and @emn178, thanks for your explanations, pretty clear to me, and it makes sense. The above fixes it: https://irwansetiawan.github.io/gs_plus/sha256_requirejs_conflict_resolved.html
However, I think I am not giving the right example. Apologies.
In my scenario, I do not want (and I can't have) sha256 loaded using requirejs.
- I have a Chrome plugin that needs to add sha256 library from the Content Script context (of the Chrome plugin) into the DOM context. The only way to add it is to create script tag.
function addScript(relative_url) {
var s = document.createElement('script');
s.src = chrome.extension.getURL(relative_url);
(document.head || document.documentElement).appendChild(s);
s.onload = function() { this.remove(); };
}
addScript('sha256.min.js');
- On the DOM context (a site that I do not control), there is requirejs loaded. In this context I will need to execute some script that depends on sha256 library.
In short, I am adding sha256 from a Chrome plugin, into any site (that could contain requirejs).
A couple of thoughts:
- Does the
define
used in sha256 refers to the requirejs'sdefine
? If not, can we rename it? - Can both sha256 and requirejs co-exists without creating dependencies?
from js-sha256.
@irwansetiawan @emn178 Well, I'm not familiar enough with the source code to be sure it won't have any side effects, but giving the anonymously defined module an ID should resolve the issue, right?
from js-sha256.
Try to detect if RequireJS exists
function addScript(relative_url) {
if (typeof define === 'function' && define.amd) {
require([chrome.extension.getURL(relative_url)]);
} else {
var s = document.createElement('script');
s.src = chrome.extension.getURL(relative_url);
(document.head || document.documentElement).appendChild(s);
s.onload = function() { this.remove(); };
}
}
from js-sha256.
@emn178 unfortunately by detecting if RequireJS exists and use require()
would not add the library into the DOM context.
@hakash you're a legend, yes adding module name should fix the compatibility issue, and I tested it here: https://irwansetiawan.github.io/gs_plus/sha256_requirejs_modulename.html
Thank you @emn178 and @hakash!!
from js-sha256.
from js-sha256.
Yes, I have tried with the unmerged file on my previous example, and it works:
https://irwansetiawan.github.io/gs_plus/sha256_requirejs_modulename.html
Thanks @hakash !
from js-sha256.
@irwansetiawan You are most welcome! I'm just glad I could help.
from js-sha256.
The difference between defined and undefined module ID:
defined
you have to add script tag first and then require(moduleID), eg.
<script src="path/sha256.js"></script>
require(['sha256'], function (sha256) {
// ...
});
undefined
you can require(path), eg.
require(['path/sha256.js'], function (sha256) {
// ...
});
or
configure RequireJS and require by your config, eg.
```JavaScript
requirejs.config({
paths: {
sha256: 'path/sha256'
}
});
require(['sha256'], function (sha256) {
// ...
});
To prevent naming conflict and flexible, it usual not to define a module ID, so I can't merge this PR.
from js-sha256.
@emn178 Cool! Thanks for the explanation, I do see your dilemma if its behaviour changes that much.
from js-sha256.
Related Issues (20)
- Crypto not found in React Native HOT 4
- Different Result with sha256.hmac HOT 1
- Rollupjs build complains about the use of eval in this library. HOT 7
- Hmac update question HOT 2
- Outdated typescript definition HOT 6
- Using Salt HOT 1
- Example for File Hash SHA256 ? HOT 1
- One of the test cases for HMAC is "wrong" HOT 1
- Library does not work under strict Content-Security-Policy HOT 2
- Different output on online-tools and while using the lib HOT 2
- How fast is it compared to native ? HOT 3
- When will the version be updated? How often is the update? HOT 3
- How to decrypt HOT 4
- 同学,您这个项目引入了22个开源组件,存在3个漏洞,辛苦升级一下 HOT 1
- Deprecate library and reccomend standardized APIs HOT 12
- Google Chrome 115.0.5790.102 produces incorrect SHA-256 hashes HOT 21
- Sha256
- Generates incorrect hash in some cases HOT 2
- is it possible generate hash asynchronously ? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from js-sha256.