Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Plasma
Plasma Browser Integration
Commits
a3e8e7a6
Commit
a3e8e7a6
authored
Nov 14, 2021
by
Ambareesh Balaji
Browse files
Use WeakSet to avoid memory leak
parent
4f52838d
Changes
1
Hide whitespace changes
Inline
Side-by-side
extension/content-script.js
View file @
a3e8e7a6
...
...
@@ -176,7 +176,7 @@ var playerCallbacks = [];
// Playback state communicated via media sessions api
var
playerPlaybackState
=
""
;
var
players
=
[]
;
var
players
=
new
WeakSet
()
;
var
pendingSeekingUpdate
=
0
;
...
...
@@ -385,11 +385,6 @@ function setPlayerActive(player) {
}
function
sendPlayerGone
()
{
var
playerIdx
=
players
.
indexOf
(
activePlayer
);
if
(
playerIdx
>
-
1
)
{
players
.
splice
(
playerIdx
,
1
);
}
activePlayer
=
undefined
;
pendingActivePlayer
=
undefined
;
playerMetadata
=
{};
...
...
@@ -411,7 +406,7 @@ function sendPlayerInfo(player, event, payload) {
}
function
registerPlayer
(
player
)
{
if
(
players
.
indexOf
(
player
)
>
-
1
)
{
if
(
players
.
has
(
player
))
{
//console.log("Already know", player);
return
;
}
...
...
@@ -507,7 +502,7 @@ function registerPlayer(player) {
});
});
players
.
push
(
player
);
players
.
add
(
player
);
}
function
findAllPlayersFromNode
(
node
)
{
...
...
@@ -564,6 +559,7 @@ function loadMpris() {
// cf. "checkPlayer" event above
var
observer
=
new
MutationObserver
(
function
(
mutations
)
{
let
nodesRemoved
=
false
;
mutations
.
forEach
(
function
(
mutation
)
{
mutation
.
addedNodes
.
forEach
(
function
(
node
)
{
if
(
typeof
node
.
matches
!==
"
function
"
)
{
...
...
@@ -581,35 +577,23 @@ function loadMpris() {
});
});
mutation
.
removedNodes
.
forEach
(
function
(
node
)
{
if
(
typeof
node
.
matches
!==
"
function
"
)
{
return
;
}
// Check whether the node itself or any of its children is the current player
var
players
=
findAllPlayersFromNode
(
node
);
if
(
node
.
matches
(
"
video,audio
"
))
{
players
.
unshift
(
node
);
}
nodesRemoved
=
nodesRemoved
||
mutation
.
removedNodes
.
length
>
0
;
});
players
.
forEach
(
function
(
player
)
{
if
(
player
==
activePlayer
)
{
// If t
he player
is still in the visible DOM, don't consider it gone
if
(
document
.
body
.
contains
(
p
layer
))
{
return
;
// continue
}
if
(
activePlayer
&&
nodesRemoved
)
{
// One of the removed nodes could be
activePlayer
// If
i
t is still in the visible DOM, don't consider it gone
if
(
document
.
body
.
contains
(
activeP
layer
))
{
return
;
}
// If the player got temporarily added by us, don't consider it gone
if
(
p
layer
.
dataset
.
pbiPausedForDomRemoval
===
"
true
"
)
{
return
;
}
// If the player got temporarily added by us, don't consider it gone
if
(
activeP
layer
.
dataset
.
pbiPausedForDomRemoval
===
"
true
"
)
{
return
;
}
sendPlayerGone
();
return
;
}
});
});
});
sendPlayerGone
();
}
});
window
.
addEventListener
(
"
pagehide
"
,
function
()
{
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment