Skip to content

Commit

Permalink
Merge pull request #20 from SMKIDRaadet/Safari-support
Browse files Browse the repository at this point in the history
Adds Safari support
  • Loading branch information
TheHarcker committed Dec 17, 2022
2 parents 4b65816 + 3b5bcc3 commit ed00eb1
Show file tree
Hide file tree
Showing 39 changed files with 1,676 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ __pycache__/
.idea/
Mozzila_submission
.DS_Store
xcuserdata/
*.xcuserstate
989 changes: 989 additions & 0 deletions Safari extension/DTU Course Analyzer.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"images" : [
{
"size" : "1024x1024",
"idiom" : "universal",
"filename" : "[email protected]",
"platform" : "ios"
},
{
"size" : "16x16",
"idiom" : "mac",
"filename" : "[email protected]",
"scale" : "1x"
},
{
"size" : "16x16",
"idiom" : "mac",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"size" : "32x32",
"idiom" : "mac",
"filename" : "[email protected]",
"scale" : "1x"
},
{
"size" : "32x32",
"idiom" : "mac",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"size" : "128x128",
"idiom" : "mac",
"filename" : "[email protected]",
"scale" : "1x"
},
{
"size" : "128x128",
"idiom" : "mac",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"size" : "256x256",
"idiom" : "mac",
"filename" : "[email protected]",
"scale" : "1x"
},
{
"size" : "256x256",
"idiom" : "mac",
"filename" : "[email protected]",
"scale" : "2x"
},
{
"size" : "512x512",
"idiom" : "mac",
"filename" : "[email protected]",
"scale" : "1x"
},
{
"size" : "512x512",
"idiom" : "mac",
"filename" : "[email protected]",
"scale" : "2x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions Safari extension/Shared (App)/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x",
"filename" : "icon128.png"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions Safari extension/Shared (App)/Base.lproj/Main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Security-Policy" content="default-src 'self'">

<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">

<link rel="stylesheet" href="../Style.css">
<script src="../Script.js" defer></script>
</head>
<body>
<img src="../Icon.png" width="128" height="128" alt="DTU Course Analyzer Icon">
<p class="platform-ios">You can turn on DTU Course Analyzer’s Safari extension in Settings.</p>
<p class="platform-mac state-unknown">You can turn on DTU Course Analyzer’s extension in Safari Extensions preferences.</p>
<p class="platform-mac state-on">DTU Course Analyzer’s extension is currently on. You can turn it off in Safari Extensions preferences.</p>
<p class="platform-mac state-off">DTU Course Analyzer’s extension is currently off. You can turn it on in Safari Extensions preferences.</p>
<button class="platform-mac open-preferences">Quit and Open Safari Extensions Preferences…</button>
</body>
</html>
Binary file added Safari extension/Shared (App)/Resources/Icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions Safari extension/Shared (App)/Resources/Script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function show(platform, enabled, useSettingsInsteadOfPreferences) {
document.body.classList.add(`platform-${platform}`);

if (useSettingsInsteadOfPreferences) {
document.getElementsByClassName('platform-mac state-on')[0].innerText = "DTU Course Analyzer’s extension is currently on. You can turn it off in the Extensions section of Safari Settings.";
document.getElementsByClassName('platform-mac state-off')[0].innerText = "DTU Course Analyzer’s extension is currently off. You can turn it on in the Extensions section of Safari Settings.";
document.getElementsByClassName('platform-mac state-unknown')[0].innerText = "You can turn on DTU Course Analyzer’s extension in the Extensions section of Safari Settings.";
document.getElementsByClassName('platform-mac open-preferences')[0].innerText = "Quit and Open Safari Settings…";
}

if (typeof enabled === "boolean") {
document.body.classList.toggle(`state-on`, enabled);
document.body.classList.toggle(`state-off`, !enabled);
} else {
document.body.classList.remove(`state-on`);
document.body.classList.remove(`state-off`);
}
}

function openPreferences() {
webkit.messageHandlers.controller.postMessage("open-preferences");
}

document.querySelector("button.open-preferences").addEventListener("click", openPreferences);
61 changes: 61 additions & 0 deletions Safari extension/Shared (App)/Resources/Style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
* {
-webkit-user-select: none;
-webkit-user-drag: none;
cursor: default;
}

:root {
color-scheme: light dark;

--spacing: 20px;
}

html {
height: 100%;
}

body {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;

gap: var(--spacing);
margin: 0 calc(var(--spacing) * 2);
height: 100%;

font: -apple-system-short-body;
text-align: center;
}

body:not(.platform-mac, .platform-ios) :is(.platform-mac, .platform-ios) {
display: none;
}

body.platform-ios .platform-mac {
display: none;
}

body.platform-mac .platform-ios {
display: none;
}

body.platform-ios .platform-mac {
display: none;
}

body:not(.state-on, .state-off) :is(.state-on, .state-off) {
display: none;
}

body.state-on :is(.state-off, .state-unknown) {
display: none;
}

body.state-off :is(.state-on, .state-unknown) {
display: none;
}

button {
font-size: 1em;
}
81 changes: 81 additions & 0 deletions Safari extension/Shared (App)/ViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//
// ViewController.swift
// Shared (App)
//
// Created by Hans Harck Tønning on 17.12.2022.
//

import WebKit

#if os(iOS)
import UIKit
typealias PlatformViewController = UIViewController
#elseif os(macOS)
import Cocoa
import SafariServices
typealias PlatformViewController = NSViewController
#endif

let extensionBundleIdentifier = "dk.smkid.courseanalyzer.Extension"

class ViewController: PlatformViewController, WKNavigationDelegate, WKScriptMessageHandler {

@IBOutlet var webView: WKWebView!

override func viewDidLoad() {
super.viewDidLoad()

self.webView.navigationDelegate = self

#if os(iOS)
self.webView.scrollView.isScrollEnabled = false
#endif

self.webView.configuration.userContentController.add(self, name: "controller")

self.webView.loadFileURL(Bundle.main.url(forResource: "Main", withExtension: "html")!, allowingReadAccessTo: Bundle.main.resourceURL!)
}

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
#if os(iOS)
webView.evaluateJavaScript("show('ios')")
#elseif os(macOS)
webView.evaluateJavaScript("show('mac')")

SFSafariExtensionManager.getStateOfSafariExtension(withIdentifier: extensionBundleIdentifier) { (state, error) in
guard let state = state, error == nil else {
// Insert code to inform the user that something went wrong.
return
}

DispatchQueue.main.async {
if #available(macOS 13, *) {
webView.evaluateJavaScript("show('mac', \(state.isEnabled), true)")
} else {
webView.evaluateJavaScript("show('mac', \(state.isEnabled), false)")
}
}
}
#endif
}

func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
#if os(macOS)
if (message.body as! String != "open-preferences") {
return;
}

SFSafariApplication.showPreferencesForExtension(withIdentifier: extensionBundleIdentifier) { error in
guard error == nil else {
// Insert code to inform the user that something went wrong.
return
}

DispatchQueue.main.async {
NSApplication.shared.terminate(nil)
}
}
#endif
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// SafariWebExtensionHandler.swift
// Shared (Extension)
//
// Created by Hans Harck Tønning on 17.12.2022.
//

import SafariServices
import os.log

let SFExtensionMessageKey = "message"

class SafariWebExtensionHandler: NSObject, NSExtensionRequestHandling {

func beginRequest(with context: NSExtensionContext) {
let item = context.inputItems[0] as! NSExtensionItem
let message = item.userInfo?[SFExtensionMessageKey]
os_log(.default, "Received message from browser.runtime.sendNativeMessage: %@", message as! CVarArg)

let response = NSExtensionItem()
response.userInfo = [ SFExtensionMessageKey: [ "Response to": message ] ]

context.completeRequest(returningItems: [response], completionHandler: nil)
}

}
24 changes: 24 additions & 0 deletions Safari extension/iOS (App)/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// AppDelegate.swift
// iOS (App)
//
// Created by Hans Harck Tønning on 17.12.2022.
//

import UIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}

func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}

}
Loading

0 comments on commit ed00eb1

Please sign in to comment.