Skip to content

Commit

Permalink
Fix brave#1419: Fullscreen for Desktop UA now works. (brave#1366)
Browse files Browse the repository at this point in the history
Tested websites: Vimeo, DailyMotion, Youtube, CouchTuner, JWPlayer.
  • Loading branch information
Brandon-T authored and jhreis committed Aug 21, 2019
1 parent 86a7e0d commit 27e1f81
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Client.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@
5E4845C022DE381200372022 /* WindowRenderHelper.js in Resources */ = {isa = PBXBuildFile; fileRef = 5E4845BF22DE381200372022 /* WindowRenderHelper.js */; };
5E4845C222DE3DF800372022 /* WindowRenderHelperScript.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E4845C122DE3DF800372022 /* WindowRenderHelperScript.swift */; };
5E9288CA22DF864C007BE7A6 /* TabSessionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E9288C922DF864C007BE7A6 /* TabSessionTests.swift */; };
5EB57D0222FDC0CB00A07325 /* FullscreenHelper.js in Resources */ = {isa = PBXBuildFile; fileRef = 5EB57D0122FDC0CB00A07325 /* FullscreenHelper.js */; };
744B0FFE1B4F172E00100422 /* ToolbarTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 744B0FFD1B4F172E00100422 /* ToolbarTests.swift */; };
744ED5611DBFEB8D00A2B5BE /* MailtoLinkHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 744ED5601DBFEB8D00A2B5BE /* MailtoLinkHandler.swift */; };
7479B4EF1C5306A200DF000B /* Reachability.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7479B4ED1C5306A200DF000B /* Reachability.swift */; };
Expand Down Expand Up @@ -1878,6 +1879,7 @@
5E4845BF22DE381200372022 /* WindowRenderHelper.js */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.javascript; path = WindowRenderHelper.js; sourceTree = "<group>"; };
5E4845C122DE3DF800372022 /* WindowRenderHelperScript.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WindowRenderHelperScript.swift; sourceTree = "<group>"; };
5E9288C922DF864C007BE7A6 /* TabSessionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TabSessionTests.swift; sourceTree = "<group>"; };
5EB57D0122FDC0CB00A07325 /* FullscreenHelper.js */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.javascript; path = FullscreenHelper.js; sourceTree = "<group>"; };
744B0FFD1B4F172E00100422 /* ToolbarTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ToolbarTests.swift; sourceTree = "<group>"; };
744ED5601DBFEB8D00A2B5BE /* MailtoLinkHandler.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MailtoLinkHandler.swift; sourceTree = "<group>"; };
7479B4ED1C5306A200DF000B /* Reachability.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Reachability.swift; path = ThirdParty/Reachability.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3575,6 +3577,7 @@
F99505FE22937E3900CC6543 /* U2F-low-level.js */,
5E3477E822D7771700B0D5F8 /* ResourceDownloader.js */,
5E4845BF22DE381200372022 /* WindowRenderHelper.js */,
5EB57D0122FDC0CB00A07325 /* FullscreenHelper.js */,
);
path = UserScripts;
sourceTree = "<group>";
Expand Down Expand Up @@ -5188,6 +5191,7 @@
FA9294011D6584A200AC8D33 /* QRCode.xcassets in Resources */,
D308EE561CBF0BF5006843F2 /* CertError.css in Resources */,
4422D4AD21BFFB7600BF1855 /* LICENSE in Resources */,
5EB57D0222FDC0CB00A07325 /* FullscreenHelper.js in Resources */,
E4B7B7641A793CF20022C5E0 /* CharisSILR.ttf in Resources */,
4422D56D21BFFB7F00BF1855 /* make_unicode_groups.py in Resources */,
E4B7B7681A793CF20022C5E0 /* FiraSans-Bold.ttf in Resources */,
Expand Down
23 changes: 23 additions & 0 deletions Client/Frontend/Browser/UserScriptManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,25 @@ class UserScriptManager {

return WKUserScript(source: alteredSource, injectionTime: .atDocumentStart, forMainFrameOnly: false)
}()

private let FullscreenHelperScript: WKUserScript? = {
guard let path = Bundle.main.path(forResource: "FullscreenHelper", ofType: "js"), let source = try? String(contentsOfFile: path) else {
log.error("Failed to load FullscreenHelper.js")
return nil
}

//Verify that the application itself is making a call to the JS script instead of other scripts on the page.
//This variable will be unique amongst scripts loaded in the page.
//When the script is called, the token is provided in order to access teh script variable.
var alteredSource = source
let token = UserScriptManager.securityToken.uuidString.replacingOccurrences(of: "-", with: "", options: .literal)
alteredSource = alteredSource.replacingOccurrences(of: "$<possiblySupportedFunctions>", with: "PSF\(token)", options: .literal)
alteredSource = alteredSource.replacingOccurrences(of: "$<isFullscreenSupportedNatively>", with: "IFSN\(token)", options: .literal)
alteredSource = alteredSource.replacingOccurrences(of: "$<documentHasFullscreenFunctions>", with: "DHFF\(token)", options: .literal)
alteredSource = alteredSource.replacingOccurrences(of: "$<videosSupportFullscreen>", with: "VSF\(token)", options: .literal)

return WKUserScript(source: alteredSource, injectionTime: .atDocumentStart, forMainFrameOnly: false)
}()

private func reloadUserScripts() {
tab?.webView?.configuration.userContentController.do {
Expand Down Expand Up @@ -179,6 +198,10 @@ class UserScriptManager {
if let script = WindowRenderHelperScript {
$0.addUserScript(script)
}

if let script = FullscreenHelperScript {
$0.addUserScript(script)
}
}
}
}
63 changes: 63 additions & 0 deletions Client/Frontend/UserContent/UserScripts/FullscreenHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */


/*var $<possiblySupportedFunctions> = {
request: ["requestFullscreen", "webkitRequestFullscreen", "webkitRequestFullScreen", "mozRequestFullScreen", "msRequestFullscreen"],
exit: ["exitFullscreen", "webkitCancelFullScreen", "webkitExitFullscreen", "mozCancelFullScreen", "msExitFullscreen"],
enabled: ["fullscreenEnabled", "webkitFullscreenEnabled", "mozFullScreenEnabled", "msFullscreenEnabled"],
element: ["fullscreenElement", "webkitFullscreenElement", "webkitCurrentFullScreenElement", "mozFullScreenElement", "msFullscreenElement"],
change: ["fullscreenchange", "webkitfullscreenchange", "mozfullscreenchange", "MSFullscreenChange"],
error: ["fullscreenerror", "webkitfullscreenerror", "mozfullscreenerror", "MSFullscreenError"]
}*/

var $<isFullscreenSupportedNatively> = document.fullscreenEnabled ||
document.webkitFullscreenEnabled ||
document.mozFullScreenEnabled ||
document.msFullscreenEnabled ? true : false;

var $<documentHasFullscreenFunctions> = document.documentElement.requestFullscreen ||
document.documentElement.webkitRequestFullscreen ||
document.documentElement.mozRequestFullScreen ||
document.documentElement.msRequestFullscreen ||
document.requestFullscreen ||
document.webkitRequestFullscreen ||
document.mozRequestFullScreen ||
document.msRequestFullscreen ? true : false;

var $<videosSupportFullscreen> = HTMLVideoElement.prototype.webkitEnterFullscreen !== undefined;

if (!$<isFullscreenSupportedNatively> && $<videosSupportFullscreen>) {

HTMLElement.prototype.requestFullscreen = function() {
if (this.webkitRequestFullscreen !== undefined) {
this.webkitRequestFullscreen();
return true;
}

if (this.webkitEnterFullscreen !== undefined) {
this.webkitEnterFullscreen();
return true;
}

var video = this.querySelector("video")
if (video !== undefined) {
video.webkitEnterFullscreen();
return true;
}
return false;
};

Object.defineProperty(document, 'fullscreenEnabled', {
get: function() {
return true;
}
});

Object.defineProperty(document.documentElement, 'fullscreenEnabled', {
get: function() {
return true;
}
});
}

0 comments on commit 27e1f81

Please sign in to comment.