问题
This is really embarassing
on virtually any site on the internet,
window.crypto.subtle
returns
SubtleCrypto {}
__proto__: SubtleCrypto
in the chrome console (v61 (Official Build) (64-bit))
except for
my webpage, and blank.org
where
window.crypto.subtle
returns
undefined
according to https://developer.mozilla.org/en-US/docs/Web/API/Crypto/subtle it's a read-only property that should always return a SubtleCrypto object.
what could I have done, or what has blank.org done that it could possibly not?
ps: in firefox it seems to work as intended on both my site and blank.org
回答1:
According to the spec (via Github issues) a la this Google page for WebCrypto:
crypto.subtle is supposed to be undefined in insecure contexts
回答2:
check your URL's
if it is
https://localhost:PORT
or 0.0.0.0:port
or 127.0.0.0:port
change it to proper hostname URL something like
http://localhost:PORT
worked for me! Thanks @Zmart
回答3:
It would appear you have to use sites with https://...... and not vanilla http://....
From the spec - easy to miss (and linked by Zmart, above):
Access to the WebCrypto API is restricted to secure origins (which is to say https:// pages).
来源:https://stackoverflow.com/questions/46468104/how-to-use-subtlecrypto-in-chrome-window-crypto-subtle-is-undefined