File: page-script.js

package info (click to toggle)
plasma-browser-integration 6.3.4-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 2,044 kB
  • sloc: cpp: 2,989; javascript: 2,444; xml: 142; python: 77; sh: 46; makefile: 16
file content (294 lines) | stat: -rw-r--r-- 13,179 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
(function() {
    let purposeTransferObject = null;
    let mprisTransferObject = null;
    let eventCallback = function(e) {
        e.stopPropagation();
        const args = e.detail;
        if (args.action == "unload") {
            // TODO: Undo other operations
            window.removeEventListener("org.kde.pbi.event", eventCallback, {"capture": true});
        } else if (args.action == "mediaSessionsRegister") {
            const MediaSessionsClassName_constructor = function() {
                this.callbacks = {};
                this.pendingCallbacksUpdate = 0;
                this.metadata = null;
                this.playbackState = "none";

                this.sendMessage = function(action, payload) {
                    let event = new CustomEvent("org.kde.pbi.mpris.message", {
                        detail: {
                            action: action,
                            payload: payload
                        }
                    });
                    window.dispatchEvent(event);
                };

                this.executeCallback = function(action) {
                    let details = {
                        action: action
                        // for seekforward, seekbackward, seekto there's additional information one would need to add
                    };
                    this.callbacks[action](details);
                };

                this.setCallback = function(name, cb) {
                    const oldCallbacks = Object.keys(this.callbacks).sort();

                    if (cb) {
                        this.callbacks[name] = cb;
                    } else {
                        delete this.callbacks[name];
                    }

                    const newCallbacks = Object.keys(this.callbacks).sort();

                    if (oldCallbacks.toString() === newCallbacks.toString()) {
                        return;
                    }

                    if (this.pendingCallbacksUpdate) {
                        return;
                    }

                    this.pendingCallbacksUpdate = setTimeout(() => {
                        this.pendingCallbacksUpdate = 0;

                        // Make sure to send the current callbacks, not "newCallbacks" at the time of starting the timeout
                        const callbacks = Object.keys(this.callbacks);
                        this.sendMessage("callbacks", callbacks);
                    }, 0);
                };

                this.setMetadata = function(metadata) {
                    // MediaMetadata is not a regular Object so we cannot just JSON.stringify it
                    let newMetadata = {};

                    let dirty = (!metadata != !this.metadata);
                    if (metadata) {
                        const keys = Object.getOwnPropertyNames(Object.getPrototypeOf(metadata));

                        const oldMetadata = this.metadata || {};

                        keys.forEach((key) => {
                            const value = metadata[key];
                            if (!value || typeof value === "function") {
                                return; // continue
                            }

                            // We only have Strings or the "artwork" Array, so a toString() comparison should suffice...
                            dirty |= (value.toString() !== (oldMetadata[key] || "").toString());

                            newMetadata[key] = value;
                        });
                    }

                    this.metadata = metadata;

                    if (dirty) {
                        this.sendMessage("metadata", newMetadata);
                    }
                };

                this.setPlaybackState = function(playbackState) {
                    if (this.playbackState === playbackState) {
                        return;
                    }

                    this.playbackState = playbackState;
                    this.sendMessage("playbackState", playbackState);
                };
            };

            mprisTransferObject = new MediaSessionsClassName_constructor();

            if (!navigator.mediaSession) {
                navigator.mediaSession = {};
            }

            var noop = function() {};

            var oldSetActionHandler = navigator.mediaSession.setActionHandler || noop;
            navigator.mediaSession.setActionHandler = function(name, cb) {
                mprisTransferObject.setCallback(name, cb);

                // Call the original native implementation
                // "call()" is needed as the real setActionHandler is a class member
                // and calling it directly is illegal as it lacks the context
                // This may throw for unsupported actions but we registered the callback
                // ourselves before
                return oldSetActionHandler.call(navigator.mediaSession, name, cb);
            };

            Object.defineProperty(navigator.mediaSession, "metadata", {
                get: () => mprisTransferObject.metadata,
                set: (newValue) => {
                    mprisTransferObject.setMetadata(newValue);
                }
            });
            Object.defineProperty(navigator.mediaSession, "playbackState", {
                get: () => mprisTransferObject.playbackState,
                set: (newValue) => {
                    mprisTransferObject.setPlaybackState(newValue);
                }
            });

            if (!window.MediaMetadata) {
                window.MediaMetadata = function(data) {
                    Object.assign(this, data);
                };
                window.MediaMetadata.prototype.title = "";
                window.MediaMetadata.prototype.artist = "";
                window.MediaMetadata.prototype.album = "";
                window.MediaMetadata.prototype.artwork = [];
            }

            // here we replace the document.createElement function with our own so we can detect
            // when an <audio> tag is created that is not added to the DOM which most pages do
            // while a <video> tag typically ends up being displayed to the user, audio is not.
            // HACK We cannot really pass variables from the page's scope to our content-script's scope
            // so we just blatantly insert the <audio> tag in the DOM and pick it up through our regular
            // mechanism. Let's see how this goes :D

            // HACK When removing a media object from DOM it is paused, so what we do here is once the
            // player loaded some data we add it (doesn't work earlier since it cannot pause when
            // there's nothing loaded to pause) to the DOM and before we remove it, we note down that
            // we will now get a paused event because of that. When we get it, we just play() the player
            // so it continues playing :-)
            let addPlayerToDomEvadingAutoPlayBlocking = function(player) {
                player.registerInDom = () => {
                    // Needs to be dataset so it's accessible from mutation observer on webpage
                    player.dataset.pbiPausedForDomRemoval = "true";
                    player.removeEventListener("play", player.registerInDom);

                    // If it is already in DOM by the time it starts playing, we don't need to do anything
                    // Also, if the page already parented it around, don't mess with it
                    if (document.documentElement.contains(player)
                        || player.parentNode) {
                        delete player.dataset.pbiPausedForDomRemoval;
                        player.removeEventListener("pause", player.replayAfterRemoval);
                    } else {
                        (document.head || document.documentElement).appendChild(player);
                        player.parentNode.removeChild(player);
                    }
                };

                player.replayAfterRemoval = () => {
                    if (player.dataset.pbiPausedForDomRemoval === "true") {
                        delete player.dataset.pbiPausedForDomRemoval;
                        player.removeEventListener("pause", player.replyAfterRemoval);

                        player.play();
                    }
                };

                player.addEventListener("play", player.registerInDom);
                player.addEventListener("pause", player.replayAfterRemoval);
            }

            const oldCreateElement = Document.prototype.createElement;
            Document.prototype.createElement = function() {
                const createdTag = oldCreateElement.apply(this, arguments);
                const tagName = arguments[0];

                if (typeof tagName === "string") {
                    if (tagName.toLowerCase() === "audio" || tagName.toLowerCase() === "video") {
                        const player = createdTag;
                        addPlayerToDomEvadingAutoPlayBlocking(player);
                    }
                }
                return createdTag;
            };

            // We also briefly add items created as new Audio() to the DOM so we can control it
            // similar to the document.createElement hack above since we cannot share variables
            // between the actual website and the background script despite them sharing the same DOM
            var oldAudio = window.Audio;
            window.Audio = function(...args) {
                const player = new oldAudio(...args);
                addPlayerToDomEvadingAutoPlayBlocking(player);
                return player;
            };
        } else if (args.action == "mpris") {
            try {
                mprisTransferObject.executeCallback(args.mprisCallbackName);
            } catch (e) {
                console.warn("Exception executing '" + args.mprisCallbackName + "' media sessions callback", e);
            }
        } else if (args.action == "purposeRegister") {
            purposeTransferObject = function() {};
            purposeTransferObject.reset = () => {
                purposeTransferObject.pendingResolve = null;
                purposeTransferObject.pendingReject = null;
            };
            purposeTransferObject.reset();

            if (!navigator.canShare) {
                navigator.canShare = (data) => {
                    if (!data) {
                        return false;
                    }

                    if (data.title === undefined && data.text === undefined && data.url === undefined) {
                        return false;
                    }

                    if (data.url) {
                        // check if URL is valid
                        try {
                            new URL(data.url, document.location.href);
                        } catch (e) {
                            return false;
                        }
                    }

                    return true;
                }
            }

            if (!navigator.share) {
                navigator.share = (data) => {
                    return new Promise((resolve, reject) => {
                        if (!navigator.canShare(data)) {
                            return reject(new TypeError());
                        }

                        if (data.url) {
                            // validity already checked in canShare, hence no catch
                            data.url = new URL(data.url, document.location.href).toString();
                        }

                        if (!window.event || !window.event.isTrusted) {
                            return reject(new DOMException("navigator.share can only be called in response to user interaction", "NotAllowedError"));
                        }

                        if (purposeTransferObject.pendingResolve || purposeTransferObject.pendingReject) {
                            return reject(new DOMException("A share is already in progress", "AbortError"));
                        }

                        purposeTransferObject.pendingResolve = resolve;
                        purposeTransferObject.pendingReject = reject;

                        const event = new CustomEvent("org.kde.pbi.purpose.message", {
                            detail: {
                                action: "share",
                                payload: data
                            }
                        });
                        window.dispatchEvent(event);
                    });
                };
            }
        } else if (args.action == "purposeShare") {
            purposeTransferObject.pendingResolve();
        } else if (args.action == "purposeReject") {
            purposeTransferObject.pendingReject(new DOMException("Share request aborted", "AbortError"));
        } else if (args.action == "purposeReset") {
            purposeTransferObject.reset();
        } else {
            console.warn("Unknown page script action" + args.action, args);
        }
    };
    window.addEventListener("org.kde.pbi.event", eventCallback, {"capture": true});
    window.dispatchEvent(new CustomEvent("org.kde.pbi.inited"));
}());