docs(auth): fix session storage restore snippets (#9970)

This commit is contained in:
Max Schmitt 2021-11-02 16:51:22 +01:00 committed by GitHub
parent a51ac39275
commit b8b4f904b2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -180,9 +180,9 @@ const sessionStorage = process.env.SESSION_STORAGE;
await context.addInitScript(storage => { await context.addInitScript(storage => {
if (window.location.hostname === 'example.com') { if (window.location.hostname === 'example.com') {
const entries = JSON.parse(storage); const entries = JSON.parse(storage);
Object.keys(entries).forEach(key => { for (const [key, value] of Object.entries(entries)) {
window.sessionStorage.setItem(key, entries[key]); window.sessionStorage.setItem(key, value);
}); }
} }
}, sessionStorage); }, sessionStorage);
``` ```
@ -197,11 +197,11 @@ String sessionStorage = System.getenv("SESSION_STORAGE");
context.addInitScript("(storage => {\n" + context.addInitScript("(storage => {\n" +
" if (window.location.hostname === 'example.com') {\n" + " if (window.location.hostname === 'example.com') {\n" +
" const entries = JSON.parse(storage);\n" + " const entries = JSON.parse(storage);\n" +
" Object.keys(entries).forEach(key => {\n" + " for (const [key, value] of Object.entries(entries)) {\n" +
" window.sessionStorage.setItem(key, entries[key]);\n" + " window.sessionStorage.setItem(key, value);\n" +
" });\n" + " };\n" +
" }\n" + " }\n" +
"})(" + sessionStorage + ")"); "})('" + sessionStorage + "')");
``` ```
```python async ```python async
@ -212,14 +212,14 @@ os.environ["SESSION_STORAGE"] = session_storage
# Set session storage in a new context # Set session storage in a new context
session_storage = os.environ["SESSION_STORAGE"] session_storage = os.environ["SESSION_STORAGE"]
await context.add_init_script("""storage => { await context.add_init_script("""(storage => {
if (window.location.hostname == 'example.com') { if (window.location.hostname === 'example.com') {
entries = JSON.parse(storage) const entries = JSON.parse(storage)
Object.keys(entries).forEach(key => { for (const [key, value] of Object.entries(entries)) {
window.sessionStorage.setItem(key, entries[key]) window.sessionStorage.setItem(key, key)
}) }
} }
}""", session_storage) })('""" + session_storage + "')")
``` ```
```python sync ```python sync
@ -230,14 +230,14 @@ os.environ["SESSION_STORAGE"] = session_storage
# Set session storage in a new context # Set session storage in a new context
session_storage = os.environ["SESSION_STORAGE"] session_storage = os.environ["SESSION_STORAGE"]
context.add_init_script("""storage => { context.add_init_script("""(storage => {
if (window.location.hostname == 'example.com') { if (window.location.hostname === 'example.com') {
entries = JSON.parse(storage) const entries = JSON.parse(storage)
Object.keys(entries).forEach(key => { for (const [key, value] of Object.entries(entries)) {
window.sessionStorage.setItem(key, entries[key]) window.sessionStorage.setItem(key, key)
}) }
} }
}""", session_storage) })('""" + session_storage + "')")
``` ```
```csharp ```csharp
@ -250,11 +250,11 @@ var loadedSessionStorage = Environment.GetEnvironmentVariable("SESSION_STORAGE")
await context.AddInitScriptAsync(@"(storage => { await context.AddInitScriptAsync(@"(storage => {
if (window.location.hostname === 'example.com') { if (window.location.hostname === 'example.com') {
const entries = JSON.parse(storage); const entries = JSON.parse(storage);
Object.keys(entries).forEach(key => { for (const [key, value] of Object.entries(entries)) {
window.sessionStorage.setItem(key, entries[key]); window.sessionStorage.setItem(key, value);
}); }
} }
})(" + loadedSessionStorage + ")"); })('" + loadedSessionStorage + "')");
``` ```
### API reference ### API reference