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

added Safari Extension source code #75

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
957 changes: 957 additions & 0 deletions Advent of Code Charts/Advent of Code Charts.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>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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>SchemeUserState</key>
<dict>
<key>Advent of Code Charts (iOS).xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
</dict>
<key>Advent of Code Charts (macOS).xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</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.
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 Advent of Code Charts/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="Advent of Code Charts Icon">
<p class="platform-ios">You can turn on Advent of Code Charts’s Safari extension in Settings.</p>
<p class="platform-mac state-unknown">You can turn on Advent of Code Charts’s extension in Safari Extensions preferences.</p>
<p class="platform-mac state-on">Advent of Code Charts’s extension is currently on. You can turn it off in Safari Extensions preferences.</p>
<p class="platform-mac state-off">Advent of Code Charts’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>
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 Advent of Code Charts/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 = "Advent of Code Charts’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 = "Advent of Code Charts’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 Advent of Code Charts’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 Advent of Code Charts/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 Advent of Code Charts/Shared (App)/ViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//
// ViewController.swift
// Shared (App)
//
// Created by Frédéric Druppel on 9/12/22.
//

import WebKit

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

let extensionBundleIdentifier = "nl.jeroenheijmans.Advent-of-Code-Charts.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 Frédéric Druppel on 9/12/22.
//

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 Advent of Code Charts/iOS (App)/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// AppDelegate.swift
// iOS (App)
//
// Created by Frédéric Druppel on 9/12/22.
//

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