Skip to content
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

fix: Add playback click event handling in WebEngineView #423

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
91 changes: 72 additions & 19 deletions main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -336,27 +336,80 @@ ApplicationWindow {
webView.url = webView.mainUrl;
}
}
function injectJS() {
splashScreen.visible = false
pulseOpacity.running = false
removeSplashTimer.running = false
webView.webChannel.registerObject( 'transport', transport )
// Try-catch to be able to return the error as result, but still throw it in the client context
// so it can be caught and reported
var injectedJS = "try { initShellComm() } " +
"catch(e) { setTimeout(function() { throw e }); e.message || JSON.stringify(e) }"
webView.runJavaScript(injectedJS, function(err) {
if (!err) {
webView.tries = 0
} else {
errorDialog.text = "User Interface could not be loaded.\n\nPlease try again later or contact the Stremio support team for assistance."
errorDialog.detailedText = err
errorDialog.visible = true

console.error(err)
function injectJS() {
splashScreen.visible = false;
pulseOpacity.running = false;
removeSplashTimer.running = false;
webView.webChannel.registerObject('transport', transport);


const injectedJs = `
(function() {
try {
let isToggling = false;

const togglePlay = async () => {
const video = document.querySelector('video');
if (video && !isToggling) {
isToggling = true;
video.paused ? await video.play() : await video.pause();
isToggling = false;
}
};

const attachListener = () => {
const video = document.querySelector('video');
if (video && !video._listenerAttached) {
video._listenerAttached = true;
video.addEventListener('click', togglePlay);
}
};

new MutationObserver((mutations) => {
mutations.forEach(() => attachListener());
}).observe(document.body, {
childList: true,
subtree: true
});

const origEmit = shellEvents.emit;
shellEvents.emit = (event, ...args) => {
console.log("shellEvents.emit:", event, args);
origEmit.call(shellEvents, event, ...args);
if (event === 'availabilityChanged') attachListener();
};

const origInit = window.initShellComm;
window.initShellComm = () => {
console.log("Calling original initShellComm...");
origInit?.();
attachListener();
};

attachListener();

} catch (e) {
console.error("Error in injected script:", e);
throw e;
}
});
}
})();
`;

webView.runJavaScript(injectedJs, function(result) {
if (!result) {
console.log("JavaScript injected successfully");
webView.tries = 0;
} else {
errorDialog.text = "User Interface could not be loaded.\n\nPlease try again later or contact the Stremio support team for assistance.";
errorDialog.detailedText = result;
errorDialog.visible = true;

console.error(result);
}
});
}


// We want to remove the splash after a minute
Timer {
Expand Down