Skip to content

Commit

Permalink
Create youtube-theater-fix.js, fixes brave/brave-browser#29591 (#118)
Browse files Browse the repository at this point in the history
* Create youtube-theater-fix.js, fixes brave/brave-browser#29591

adds a scriptlet to mirror the session cookie value in localstorage

* Update youtube-theater-fix.js

* Fix youtube scriptlet, add build output

* Store cookie as a JSON string

* Only set localstorage if cookie exists, else delete localstorage entry

To make sure that they are in sync

---------

Co-authored-by: Shivan Kaul Sahib <shivankaulsahib@gmail.com>
  • Loading branch information
pes10k and ShivanKaul committed Jul 7, 2023
1 parent 5a949d7 commit 6075775
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dist/resources.json

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions metadata.json
Expand Up @@ -55,6 +55,14 @@
},
"resourcePath": "brave-set-session-storage-item.js"
},
{
"name": "brave-youtube-theater-fix",
"aliases": [],
"kind": {
"mime": "application/javascript"
},
"resourcePath": "brave-youtube-theater-fix.js"
},
{
"name": "de-amp",
"aliases": [],
Expand Down
30 changes: 30 additions & 0 deletions resources/brave-youtube-theater-fix.js
@@ -0,0 +1,30 @@
(async _ => {
if (self.cookieStore === undefined) {
// Do nothing on iOS. The problem doesn't exist there, and the
// solution wouldn't work anyway
return
}
const storeKey = 'brave::wide'
const cookieKey = 'wide'
// If we have a localStorage value, set it as a cookie.
if (localStorage.getItem(storeKey) !== null) {
// The cookie object is stored as a string. Parse it.
const cookieObj = JSON.parse(localStorage.getItem(storeKey))
await cookieStore.set(cookieObj)
}

// Persist the cookie value to localStorage every second.
setInterval(async _ => {
try {
const wideCookie = await cookieStore.get(cookieKey)
// We have to stringify the cookie object to store it in localStorage.
if (wideCookie) {
localStorage.setItem(storeKey, JSON.stringify(wideCookie))
} else {
localStorage.removeItem(storeKey)
}
} catch (e) {
// swallow error from no cookie existing
}
}, 1000)
})()

0 comments on commit 6075775

Please sign in to comment.