Skip to content

clear storage on load+unload #492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/special-pages/pages/duckplayer/src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
} from '../../../../../messaging/index.js'
import { DuckPlayerPageMessages, UserValues } from './messages'
import { html } from '../../../../../../src/dom-utils'
import { initStorage } from './storage'

// for docs
export { DuckPlayerPageMessages, UserValues }
Expand Down Expand Up @@ -761,3 +762,5 @@ document.addEventListener('DOMContentLoaded', () => {
})
MouseMove.init()
})

initStorage()
32 changes: 32 additions & 0 deletions packages/special-pages/pages/duckplayer/src/js/storage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
function deleteStorage (subject) {
Object.keys(subject).forEach((key) => {
if (key.indexOf('yt-player') === 0) {
return
}
subject.removeItem(key)
})
}

function deleteAllCookies () {
const cookies = document.cookie.split(';')
for (let i = 0; i < cookies.length; i++) {
const cookie = cookies[i]
const eqPos = cookie.indexOf('=')
const name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie
document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:00 GMT;domain=youtube-nocookie.com;path=/;'
}
}

export function initStorage () {
window.addEventListener('unload', () => {
deleteStorage(localStorage)
deleteStorage(sessionStorage)
deleteAllCookies()
})

window.addEventListener('load', () => {
deleteStorage(localStorage)
deleteStorage(sessionStorage)
deleteAllCookies()
})
}
6 changes: 6 additions & 0 deletions packages/special-pages/tests/duckplayer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ test.describe('duckplayer iframe', () => {
await duckplayer.hasShownErrorMessage()
await duckplayer.hasNotAddedIframe()
})
test('clears storage', async ({ page }, workerInfo) => {
const duckplayer = DuckPlayerPage.create(page, workerInfo)
await duckplayer.openWithVideoID()
await duckplayer.withStorageValues()
await duckplayer.storageClearedAfterReload()
})
})

test.describe('duckplayer toolbar', () => {
Expand Down
15 changes: 15 additions & 0 deletions packages/special-pages/tests/page-objects/duck-player.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,21 @@ export class DuckPlayerPage {
})
}

async withStorageValues () {
await this.page.evaluate(() => {
localStorage.setItem('foo', 'bar')
localStorage.setItem('yt-player-other', 'baz')
})
}

async storageClearedAfterReload () {
await this.page.reload()
const storaget = await this.page.evaluate(() => localStorage)
expect(storaget).toMatchObject({
'yt-player-other': 'baz'
})
}

/**
* We test the fully built artifacts, so for each test run we need to
* select the correct HTML file.
Expand Down