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
4 changes: 4 additions & 0 deletions xcode/Userscripts.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
030C64F62AC62CC900548FBD /* Settings.bundle in Resources */ = {isa = PBXBuildFile; fileRef = 030C64F52AC62CC900548FBD /* Settings.bundle */; };
030C64FC2AD0B24C00548FBD /* Initialization.swift in Sources */ = {isa = PBXBuildFile; fileRef = 030C64FB2AD0B24C00548FBD /* Initialization.swift */; };
0336619F294DF7C900CFE179 /* Functions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0336619E294DF7C900CFE179 /* Functions.swift */; };
033661A529510B7900CFE179 /* View.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 033661A329510B7900CFE179 /* View.storyboard */; };
0376905B29808EEA00F3474D /* background.js in Resources */ = {isa = PBXBuildFile; fileRef = 0376905A29808EEA00F3474D /* background.js */; };
Expand Down Expand Up @@ -106,6 +107,7 @@

/* Begin PBXFileReference section */
030C64F52AC62CC900548FBD /* Settings.bundle */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.plug-in"; path = Settings.bundle; sourceTree = "<group>"; };
030C64FB2AD0B24C00548FBD /* Initialization.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Initialization.swift; sourceTree = "<group>"; };
0336619E294DF7C900CFE179 /* Functions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Functions.swift; sourceTree = "<group>"; };
033661A429510B7900CFE179 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/View.storyboard; sourceTree = "<group>"; };
0376905A29808EEA00F3474D /* background.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = background.js; sourceTree = "<group>"; };
Expand Down Expand Up @@ -210,6 +212,7 @@
4A4CF6DD270A38BD00111584 /* AppDelegate.swift */,
4A4CF6E7270A38BD00111584 /* SceneDelegate.swift */,
4A4CF6E9270A38BD00111584 /* ViewController.swift */,
030C64FB2AD0B24C00548FBD /* Initialization.swift */,
4A4CF6EB270A38BD00111584 /* LaunchScreen.storyboard */,
4A4CF6EE270A38BD00111584 /* Main.storyboard */,
);
Expand Down Expand Up @@ -566,6 +569,7 @@
4A4CF6EA270A38BD00111584 /* ViewController.swift in Sources */,
4A4CF6DE270A38BD00111584 /* AppDelegate.swift in Sources */,
03C24F992ABD2CBB00F130F9 /* Preferences.swift in Sources */,
030C64FC2AD0B24C00548FBD /* Initialization.swift in Sources */,
78B25585299D4C27000B2E9B /* Shared.swift in Sources */,
4A4CF6E8270A38BD00111584 /* SceneDelegate.swift in Sources */,
);
Expand Down
7 changes: 1 addition & 6 deletions xcode/iOS-App/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let logger = USLogger(#fileID)
// set the scripts directory to the app document on first use
if Preferences.scriptsDirectoryUrl == getDefaultScriptsDirectoryUrl() {
logger?.info("\(#function, privacy: .public) - Initialize default directory")
Preferences.scriptsDirectoryUrl = getDocumentsDirectory()
}
initializeFirstStart()
return true
}

Expand Down
47 changes: 47 additions & 0 deletions xcode/iOS-App/Initialization.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import Foundation

private let logger = USLogger(#fileID)

private func createDemoScript() {
let demoScript = """
// ==UserScript==
// @name Demo user script
// @description I am a demo user script that you can safely delete (add any files to this folder and I will no longer automatically generate)
// @author Userscripts
// @version 0.0.1
// @match *://*/*
// @grant none
// @inject-into content
// ==/UserScript==

(function () {
'use strict';
// here is your code
})();
"""
let url = getDocumentsDirectory()
// get a list of non-hidden files
guard let contents = try? FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: [], options: .skipsHiddenFiles) else {
logger?.error("\(#function, privacy: .public) - failed get contentsOfDirectory")
return
}
if contents.isEmpty {
let fileURL = url.appendingPathComponent("Demo_user_script.user.js")
do {
logger?.info("\(#function, privacy: .public) - try to write demo user script")
try demoScript.write(to: fileURL, atomically: true, encoding: .utf8)
} catch {
logger?.error("\(#function, privacy: .public) - failed to write demo user script")
}
}
}

func initializeFirstStart() {
// set the scripts directory to the app document on first use
if Preferences.scriptsDirectoryUrl == getDefaultScriptsDirectoryUrl() {
logger?.info("\(#function, privacy: .public) - Initialize default directory")
Preferences.scriptsDirectoryUrl = getDocumentsDirectory()
}
// put a visible file to display the documents directory in files app
createDemoScript()
}
2 changes: 1 addition & 1 deletion xcode/iOS-App/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class ViewController: UIViewController, WKNavigationDelegate, WKScriptMessageHan
logger?.info("\(#function, privacy: .public) - Userscripts iOS has requested to set the readLocation")
let documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: [.folder])
documentPicker.delegate = self
documentPicker.directoryURL = Preferences.scriptsDirectoryUrl
documentPicker.directoryURL = getDocumentsDirectory()
present(documentPicker, animated: true, completion: nil)
}
}
Expand Down