Skip to content
Merged
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
15 changes: 7 additions & 8 deletions src/popup/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -545,13 +545,14 @@
<div class="none">Popup inactive on extension page</div>
{:else if firstGuide}
<div class="none">
Welcome, first please:&nbsp;
<p>Welcome, first use please:&nbsp;</p>
<button
class="link"
on:click={openContainingApp}
>
set directory
Open Userscripts App
</button>
<p>to complete the initialization</p>
</div>
{:else if initError}
<div class="none">
Expand Down Expand Up @@ -716,13 +717,11 @@
}

.none {
align-items: center;
color: var(--text-color-disabled);
display: flex;
font-weight: 600;
justify-content: center;
inset: 0;
position: absolute;
color: var(--text-color-disabled);
text-align: center;
padding: 3rem 0;
line-height: 2.5;
}

.items.disabled {
Expand Down
20 changes: 20 additions & 0 deletions xcode/Mac-App/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
private var windowForego = false
private var windowLoaded = false

@IBOutlet weak var enbaleNativeLogger: NSMenuItem!

func application(_ application: NSApplication, open urls: [URL]) {
// if open panel is already open, stop processing the URL scheme
if NSApplication.shared.keyWindow?.accessibilityIdentifier() == "open-panel" { return }
Expand All @@ -26,6 +28,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

func applicationDidFinishLaunching(_ aNotification: Notification) {
// Initialize menu items
enbaleNativeLogger.state = Preferences.enableLogger ? .on : .off
if windowForego { return }
let storyboard = NSStoryboard(name: "View", bundle: Bundle.main)
let windowController = storyboard.instantiateInitialController() as! NSWindowController
Expand All @@ -47,4 +51,20 @@ class AppDelegate: NSObject, NSApplicationDelegate {
return true
}

@IBAction func enableLogger(_ sender: NSMenuItem) {
if sender.state == .on {
Preferences.enableLogger = false
sender.state = .off
} else {
Preferences.enableLogger = true
sender.state = .on
}
}

@IBAction func applicationHelp(_ sender: NSMenuItem) {
if let url = URL(string: "https://github.com/quoid/userscripts") {
NSWorkspace.shared.open(url)
}
}

}
35 changes: 32 additions & 3 deletions xcode/Mac-App/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="21507" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="22154" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="21507"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="22154"/>
</dependencies>
<scenes>
<!--Application-->
Expand Down Expand Up @@ -30,13 +30,42 @@
</items>
</menu>
</menuItem>
<menuItem title="Debug" id="tuz-YH-a2Y">
<modifierMask key="keyEquivalentModifierMask"/>
<menu key="submenu" title="Debug" id="GRz-CN-GnB">
<items>
<menuItem title="Enable Native Logger" id="5cW-8c-Ohd">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="enableLogger:" target="Voe-Tx-rLC" id="glu-66-yci"/>
</connections>
</menuItem>
</items>
</menu>
</menuItem>
<menuItem title="Help" id="aoZ-6F-67z">
<modifierMask key="keyEquivalentModifierMask"/>
<menu key="submenu" title="Help" systemMenu="help" id="Bqa-wP-Wpe">
<items>
<menuItem title="Userscripts Help" keyEquivalent="?" id="A2Q-KZ-1Q8">
<connections>
<action selector="applicationHelp:" target="Voe-Tx-rLC" id="qqf-rl-2LE"/>
</connections>
</menuItem>
</items>
</menu>
</menuItem>
</items>
</menu>
<connections>
<outlet property="delegate" destination="Voe-Tx-rLC" id="PrD-fu-P6m"/>
</connections>
</application>
<customObject id="Voe-Tx-rLC" customClass="AppDelegate" customModule="Userscripts" customModuleProvider="target"/>
<customObject id="Voe-Tx-rLC" customClass="AppDelegate" customModule="Userscripts" customModuleProvider="target">
<connections>
<outlet property="enbaleNativeLogger" destination="5cW-8c-Ohd" id="erU-m9-6bz"/>
</connections>
</customObject>
<customObject id="YLy-65-1bz" customClass="NSFontManager"/>
<customObject id="Ady-hI-5gd" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
</objects>
Expand Down
42 changes: 7 additions & 35 deletions xcode/Mac-App/Functions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,10 @@ import AppKit
import SafariServices
import os

let extensionIdentifier = Bundle.main.infoDictionary?["US_EXT_IDENTIFIER"] as! String
fileprivate let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: #fileID)
private let logger = USLogger(#fileID)

func getSaveLocationURL() -> URL {
var url: URL
// default url
if #available(macOS 13.0, *) {
url = getDocumentsDirectory().appending(path: "scripts")
} else {
url = getDocumentsDirectory().appendingPathComponent("scripts")
}
// if not in safari extension environment, replace with extension path
if let bundleIdentifier = Bundle.main.bundleIdentifier, bundleIdentifier != extensionIdentifier {
let s = url.absoluteString.replacingOccurrences(of: bundleIdentifier, with: extensionIdentifier)
// avoid being encode again, decode first
if let decodePath = s.removingPercentEncoding {
url = URL(fileURLWithPath: decodePath, isDirectory: true)
}
}
// bookmark url
if let data = UserDefaults(suiteName: SharedDefaults.suiteName)?.data(forKey: SharedDefaults.keyName) {
if let bookmarkURL = readBookmark(data: data, isSecure: false) {
if directoryExists(path: bookmarkURL.path) {
url = bookmarkURL
} else {
UserDefaults(suiteName: SharedDefaults.suiteName)?.removeObject(forKey: SharedDefaults.keyName)
NSLog("removed shared bookmark because it's directory is non-existent, permanently deleted or in trash")
}
}
}
return url
return Preferences.scriptsDirectoryUrl
}

func setSaveLocationURL(url: URL) -> Bool {
Expand All @@ -42,17 +15,14 @@ func setSaveLocationURL(url: URL) -> Bool {
alert.runModal()
return false
}
guard saveBookmark(url: url, isShared: true, keyName: SharedDefaults.keyName, isSecure: false) else {
logger.error("\(#function, privacy: .public) - couldn't save new location from host app")
return false
}
return true
Preferences.scriptsDirectoryUrl = url
return Preferences.scriptsDirectoryUrl == url
}

func sendExtensionMessage(name: String, userInfo: [String : Any]? = nil, completion: ((Error?) -> Void)? = nil) {
SFSafariApplication.dispatchMessage(
withName: name,
toExtensionWithIdentifier: extensionIdentifier,
toExtensionWithIdentifier: extIdentifier,
userInfo: userInfo
) { error in // always be called
if error != nil {
Expand Down Expand Up @@ -83,6 +53,8 @@ func schemeChangeSaveLocation() {
let response = panel.runModal()
// check if clicked open button and there is a valid result
guard response == .OK, let url: URL = panel.urls.first else { return }
// revoke implicitly starts security-scoped access
defer { url.stopAccessingSecurityScopedResource() }
// check if path has indeed changed
if url.absoluteString == saveLocationURL.absoluteString { return }
// try set new save location path to bookmark
Expand Down
13 changes: 7 additions & 6 deletions xcode/Mac-App/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Cocoa
import SafariServices.SFSafariApplication
import os

fileprivate let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: #fileID)
private let logger = USLogger(#fileID)

class ViewController: NSViewController {

Expand All @@ -14,7 +14,6 @@ class ViewController: NSViewController {

let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "??"
let buildNumber = Bundle.main.infoDictionary?["CFBundleVersion"] as? String ?? "??"
let extensionID = extensionIdentifier

override func viewDidLoad() {
super.viewDidLoad()
Expand All @@ -35,13 +34,13 @@ class ViewController: NSViewController {
}

@objc func setExtensionState() {
SFSafariExtensionManager.getStateOfSafariExtension(withIdentifier: extensionID) { (state, error) in
SFSafariExtensionManager.getStateOfSafariExtension(withIdentifier: extIdentifier) { (state, error) in
guard let state = state else {
self.enabledText.stringValue = "Safari Extension State Unknown"
if let error = error {
logger.error("\(#function, privacy: .public) - \(error.localizedDescription, privacy: .public)")
logger?.error("\(#function, privacy: .public) - \(error.localizedDescription, privacy: .public)")
} else {
logger.error("\(#function, privacy: .public) - couldn't get safari extension state in containing app")
logger?.error("\(#function, privacy: .public) - couldn't get safari extension state in containing app")
}
return
}
Expand All @@ -59,6 +58,8 @@ class ViewController: NSViewController {
panel.beginSheetModal(for: window, completionHandler: { response in
// check if clicked open button and there is a valid result
guard response == .OK, let url: URL = panel.urls.first else { return }
// revoke implicitly starts security-scoped access
defer { url.stopAccessingSecurityScopedResource() }
// check if path has indeed changed
if url.absoluteString == saveLocationURL.absoluteString { return }
// try set new save location path to bookmark
Expand All @@ -78,6 +79,6 @@ class ViewController: NSViewController {
}

@IBAction func openSafariExtensionPreferences(_ sender: AnyObject?) {
SFSafariApplication.showPreferencesForExtension(withIdentifier: extensionID)
SFSafariApplication.showPreferencesForExtension(withIdentifier: extIdentifier)
}
}
Loading