Skip to content

Commit

Permalink
added checkbox to enable custom PiP buttons and the logic for it. Als…
Browse files Browse the repository at this point in the history
…o included some changes from @kabiroberai's commit 7c0acda
  • Loading branch information
arnoappenzeller committed Oct 16, 2016
1 parent b092ccc commit 2d783e0
Show file tree
Hide file tree
Showing 14 changed files with 248 additions and 73 deletions.
21 changes: 21 additions & 0 deletions PiP_Toolbar_Icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PiP_Toolbar_Icon_white.pdf
Binary file not shown.
21 changes: 21 additions & 0 deletions PiP_Toolbar_Icon_white.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 8 additions & 4 deletions PiPifier-Safari-Extension/PiPifier_Safari_Extension.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.files.user-selected.read-only</key>
<true/>
<key>com.apple.security.app-sandbox</key>
<true/>
<key>com.apple.security.application-groups</key>
<array>
<string>group.APPenzeller.PiPifier</string>
</array>
<key>com.apple.security.files.user-selected.read-only</key>
<true/>
</dict>
</plist>
24 changes: 17 additions & 7 deletions PiPifier-Safari-Extension/SafariExtensionHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@
import SafariServices

enum Message: String {
case videoFound, noVideoFound
case videoFound, noVideoFound, shouldCustomPiPButtonsBeAdded
}

class SafariExtensionHandler: SFSafariExtensionHandler {

var manualDeactivation = false

var stateManager = StateManager.shared

override func messageReceived(withName messageName: String, from page: SFSafariPage, userInfo: [String : Any]? = nil) {
guard let message = Message(rawValue: messageName) else {
NSLog("unhandled message")
Expand All @@ -29,11 +25,14 @@ class SafariExtensionHandler: SFSafariExtensionHandler {
updateState(of: page, videoFound: true)
case .noVideoFound:
updateState(of: page, videoFound: false)
case .shouldCustomPiPButtonsBeAdded:
checkForCustomPiPButtonSetting()
}
}


func updateState(of page: SFSafariPage, videoFound: Bool) {
stateManager.videosFound[page] = videoFound
StateManager.shared.videosFound[page] = videoFound
SFSafariApplication.setToolbarItemsNeedUpdate()
}

Expand All @@ -48,13 +47,24 @@ class SafariExtensionHandler: SFSafariExtensionHandler {
override func validateToolbarItem(in window: SFSafariWindow, validationHandler: @escaping (Bool, String) -> Void) {
getActivePage {
guard let page = $0 else {return}
let videoFound = self.stateManager.videosFound[page] ?? false

let videoFound = StateManager.shared.videosFound[page] ?? false
validationHandler(videoFound, "")
}
}

func getActivePage(completionHandler: @escaping (SFSafariPage?) -> Void) {
SFSafariApplication.getActiveWindow {$0?.getActiveTab {$0?.getActivePage(completionHandler: completionHandler)}}
}

//MARK: - customPiPButton methods

func checkForCustomPiPButtonSetting(){
if SettingsManager.shared.isCustomPiPButtonsEnabled{
getActivePage {
$0?.dispatchMessageToScript(withName: "addCustomPiPButtons", userInfo: nil)
}
}
}

}
31 changes: 20 additions & 11 deletions PiPifier-Safari-Extension/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ safari.self.addEventListener("message", messageHandler); // Message recieved fro
safari.self.addEventListener("activate", checkForVideo); // Tab changed
new MutationObserver(checkForVideo).observe(document, {subtree: true, childList: true}); // DOM changed

console.log(safari.extension.baseURI + 'PiP_Toolbar_Icon_white.svg');



function dispatchMessage(messageName) {
Expand All @@ -21,27 +21,21 @@ function messageHandler(event) {
if (event.name === "enablePiP" && getVideo() != null) {
getVideo().webkitSetPresentationMode('picture-in-picture');
}
else if (event.name === "addCustomPiPButtons"){
checkForCustomPiPButtonSupport();
}
}

var firstCheck = true;
var previousResult = false;

function checkForNativeButtonSupport(){
//add custom eventListener for youtube
if (isYoutube.map(function(obj){return location.hostname.match(obj) != null;}).indexOf(true) >= 0){
addYouTubeVideoButton();
}
//check for other players
//TODO: add other players here
}


function checkForVideo() {
if (getVideo() != null) {
if (!previousResult) {
previousResult = true;
console.log("Found a video");
checkForNativeButtonSupport();
shouldCustomPiPButtonsBeAdded();
dispatchMessage("videoFound");
}
} else if (window == window.top) {
Expand All @@ -61,6 +55,21 @@ function getVideo() {

//----------------- Custom Button Methods -----------------

function shouldCustomPiPButtonsBeAdded(){
dispatchMessage("shouldCustomPiPButtonsBeAdded");
}

function checkForCustomPiPButtonSupport(){
//add custom eventListener for youtube
if (isYoutube.map(function(obj){return location.hostname.match(obj) != null;}).indexOf(true) >= 0){
addYouTubeVideoButton();
}
//check for other players
//TODO: add other players here
}


//----------------- Player Implementations -------------------------
function addYouTubeVideoButton() {
var video = document.getElementsByTagName('video')[0];

Expand Down
20 changes: 20 additions & 0 deletions PiPifier.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

/* Begin PBXBuildFile section */
110895A31D54DCBE00BF3465 /* StateManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 110895A21D54DCBE00BF3465 /* StateManager.swift */; };
115B6C241DB38BD100AE9B6F /* SettingsManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 115B6C231DB38BD100AE9B6F /* SettingsManager.swift */; };
115B6C251DB38BD100AE9B6F /* SettingsManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 115B6C231DB38BD100AE9B6F /* SettingsManager.swift */; };
11B4E1B81DB2F56C00007495 /* PiP_Toolbar_Icon_white.svg in Resources */ = {isa = PBXBuildFile; fileRef = 11B4E1B61DB2F56C00007495 /* PiP_Toolbar_Icon_white.svg */; };
11B4E1B91DB2F56C00007495 /* PiP_Toolbar_Icon.svg in Resources */ = {isa = PBXBuildFile; fileRef = 11B4E1B71DB2F56C00007495 /* PiP_Toolbar_Icon.svg */; };
11F1B5021D4EC0CD0041579B /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 11F1B5011D4EC0CD0041579B /* AppDelegate.swift */; };
Expand Down Expand Up @@ -47,6 +49,8 @@

/* Begin PBXFileReference section */
110895A21D54DCBE00BF3465 /* StateManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StateManager.swift; sourceTree = "<group>"; };
114E8EE41DB386E400C22D36 /* PiPifier.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = PiPifier.entitlements; sourceTree = "<group>"; };
115B6C231DB38BD100AE9B6F /* SettingsManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SettingsManager.swift; path = PiPifier/SettingsManager.swift; sourceTree = "<group>"; };
11B4E1B61DB2F56C00007495 /* PiP_Toolbar_Icon_white.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = PiP_Toolbar_Icon_white.svg; sourceTree = "<group>"; };
11B4E1B71DB2F56C00007495 /* PiP_Toolbar_Icon.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = PiP_Toolbar_Icon.svg; sourceTree = "<group>"; };
11F1B4FE1D4EC0CD0041579B /* PiPifier.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = PiPifier.app; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -86,6 +90,7 @@
11F1B4F51D4EC0CC0041579B = {
isa = PBXGroup;
children = (
115B6C231DB38BD100AE9B6F /* SettingsManager.swift */,
11F1B5001D4EC0CD0041579B /* PiPifier */,
11F1B5181D4EC1010041579B /* PiPifier-Safari-Extension */,
11F1B5151D4EC1010041579B /* Frameworks */,
Expand All @@ -105,6 +110,7 @@
11F1B5001D4EC0CD0041579B /* PiPifier */ = {
isa = PBXGroup;
children = (
114E8EE41DB386E400C22D36 /* PiPifier.entitlements */,
11F1B5011D4EC0CD0041579B /* AppDelegate.swift */,
11F1B5031D4EC0CD0041579B /* ViewController.swift */,
11F1B5051D4EC0CD0041579B /* Assets.xcassets */,
Expand Down Expand Up @@ -199,12 +205,22 @@
DevelopmentTeam = AW9CBV6SY7;
DevelopmentTeamName = "Arno Appenzeller";
ProvisioningStyle = Automatic;
SystemCapabilities = {
com.apple.ApplicationGroups.Mac = {
enabled = 1;
};
};
};
11F1B5131D4EC1010041579B = {
CreatedOnToolsVersion = 8.0;
DevelopmentTeam = AW9CBV6SY7;
DevelopmentTeamName = "Arno Appenzeller";
ProvisioningStyle = Automatic;
SystemCapabilities = {
com.apple.ApplicationGroups.Mac = {
enabled = 1;
};
};
};
};
};
Expand Down Expand Up @@ -257,6 +273,7 @@
files = (
11F1B5041D4EC0CD0041579B /* ViewController.swift in Sources */,
11F1B5021D4EC0CD0041579B /* AppDelegate.swift in Sources */,
115B6C241DB38BD100AE9B6F /* SettingsManager.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -266,6 +283,7 @@
files = (
11F1B51C1D4EC1010041579B /* SafariExtensionHandler.swift in Sources */,
110895A31D54DCBE00BF3465 /* StateManager.swift in Sources */,
115B6C251DB38BD100AE9B6F /* SettingsManager.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -382,6 +400,7 @@
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_ENTITLEMENTS = PiPifier/PiPifier.entitlements;
CODE_SIGN_IDENTITY = "Mac Developer";
COMBINE_HIDPI_IMAGES = YES;
DEVELOPMENT_TEAM = AW9CBV6SY7;
Expand All @@ -398,6 +417,7 @@
buildSettings = {
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_ENTITLEMENTS = PiPifier/PiPifier.entitlements;
CODE_SIGN_IDENTITY = "Mac Developer";
COMBINE_HIDPI_IMAGES = YES;
DEVELOPMENT_TEAM = AW9CBV6SY7;
Expand Down
Loading

0 comments on commit 2d783e0

Please sign in to comment.