Skip to content

Commit

Permalink
🍭 自定义匹配关键字和验证码提取正则表达式
Browse files Browse the repository at this point in the history
  • Loading branch information
DreamSaddle committed Jan 10, 2024
1 parent eb1ccae commit c4035c0
Show file tree
Hide file tree
Showing 8 changed files with 354 additions and 45 deletions.
33 changes: 33 additions & 0 deletions MacCopier.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@
AA40E1812869552B000875C3 /* MacCopierTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA40E1802869552B000875C3 /* MacCopierTests.swift */; };
AA40E18B2869552B000875C3 /* MacCopierUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA40E18A2869552B000875C3 /* MacCopierUITests.swift */; };
AA40E18D2869552B000875C3 /* MacCopierUITestsLaunchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA40E18C2869552B000875C3 /* MacCopierUITestsLaunchTests.swift */; };
AA4496DB2B4D62BA004EA9A9 /* GeneralSettingViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = AA4496DA2B4D62BA004EA9A9 /* GeneralSettingViewController.xib */; };
AA4496DD2B4D62EB004EA9A9 /* GeneralSettingViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA4496DC2B4D62EB004EA9A9 /* GeneralSettingViewController.swift */; };
AA8299FF2879BA0200FCA4BE /* Sauce in Frameworks */ = {isa = PBXBuildFile; productRef = AA8299FE2879BA0200FCA4BE /* Sauce */; };
AA829A012879BB2900FCA4BE /* Clipboard.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA829A002879BB2900FCA4BE /* Clipboard.swift */; };
AAA80DD12B4EC10200B36D7C /* RegexSettingViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = AAA80DD02B4EC10200B36D7C /* RegexSettingViewController.xib */; };
AAA80DD32B4EC11400B36D7C /* RegexSettingViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AAA80DD22B4EC11400B36D7C /* RegexSettingViewController.swift */; };
AAD54C722B4987AC00BF113E /* Settings in Frameworks */ = {isa = PBXBuildFile; productRef = AAD54C712B4987AC00BF113E /* Settings */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -53,14 +58,19 @@
AA40E1862869552B000875C3 /* MacCopierUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = MacCopierUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
AA40E18A2869552B000875C3 /* MacCopierUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MacCopierUITests.swift; sourceTree = "<group>"; };
AA40E18C2869552B000875C3 /* MacCopierUITestsLaunchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MacCopierUITestsLaunchTests.swift; sourceTree = "<group>"; };
AA4496DA2B4D62BA004EA9A9 /* GeneralSettingViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = GeneralSettingViewController.xib; sourceTree = "<group>"; };
AA4496DC2B4D62EB004EA9A9 /* GeneralSettingViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GeneralSettingViewController.swift; sourceTree = "<group>"; };
AA829A002879BB2900FCA4BE /* Clipboard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Clipboard.swift; sourceTree = "<group>"; };
AAA80DD02B4EC10200B36D7C /* RegexSettingViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = RegexSettingViewController.xib; sourceTree = "<group>"; };
AAA80DD22B4EC11400B36D7C /* RegexSettingViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RegexSettingViewController.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
AA40E16828695528000875C3 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
AAD54C722B4987AC00BF113E /* Settings in Frameworks */,
AA8299FF2879BA0200FCA4BE /* Sauce in Frameworks */,
AA0B3572287020F400D5331F /* LaunchAtLogin in Frameworks */,
);
Expand Down Expand Up @@ -124,6 +134,10 @@
AA40E1772869552B000875C3 /* MacCopier.entitlements */,
AA1F6EEF286FD3BE00350040 /* About.swift */,
AA829A002879BB2900FCA4BE /* Clipboard.swift */,
AA4496DA2B4D62BA004EA9A9 /* GeneralSettingViewController.xib */,
AA4496DC2B4D62EB004EA9A9 /* GeneralSettingViewController.swift */,
AAA80DD02B4EC10200B36D7C /* RegexSettingViewController.xib */,
AAA80DD22B4EC11400B36D7C /* RegexSettingViewController.swift */,
);
path = MacCopier;
sourceTree = "<group>";
Expand Down Expand Up @@ -172,6 +186,7 @@
packageProductDependencies = (
AA0B3571287020F400D5331F /* LaunchAtLogin */,
AA8299FE2879BA0200FCA4BE /* Sauce */,
AAD54C712B4987AC00BF113E /* Settings */,
);
productName = MacCopier;
productReference = AA40E16B28695528000875C3 /* MacCopier.app */;
Expand Down Expand Up @@ -248,6 +263,7 @@
packageReferences = (
AA0B3570287020F400D5331F /* XCRemoteSwiftPackageReference "LaunchAtLogin" */,
AA8299FD2879BA0200FCA4BE /* XCRemoteSwiftPackageReference "Sauce" */,
AAD54C702B4987AC00BF113E /* XCRemoteSwiftPackageReference "Settings" */,
);
productRefGroup = AA40E16C28695528000875C3 /* Products */;
projectDirPath = "";
Expand All @@ -266,7 +282,9 @@
buildActionMask = 2147483647;
files = (
AA30750E28702C140070C8AC /* .gitignore in Resources */,
AAA80DD12B4EC10200B36D7C /* RegexSettingViewController.xib in Resources */,
AA40E1732869552B000875C3 /* Assets.xcassets in Resources */,
AA4496DB2B4D62BA004EA9A9 /* GeneralSettingViewController.xib in Resources */,
AA40E1762869552B000875C3 /* Main.storyboard in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -313,7 +331,9 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
AA4496DD2B4D62EB004EA9A9 /* GeneralSettingViewController.swift in Sources */,
AA40E17128695528000875C3 /* ViewController.swift in Sources */,
AAA80DD32B4EC11400B36D7C /* RegexSettingViewController.swift in Sources */,
AA1F6EF0286FD3BE00350040 /* About.swift in Sources */,
AA829A012879BB2900FCA4BE /* Clipboard.swift in Sources */,
AA40E16F28695528000875C3 /* AppDelegate.swift in Sources */,
Expand Down Expand Up @@ -681,6 +701,14 @@
minimumVersion = 2.0.0;
};
};
AAD54C702B4987AC00BF113E /* XCRemoteSwiftPackageReference "Settings" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://gitee.com/taohan/Settings.git";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 3.1.0;
};
};
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
Expand All @@ -694,6 +722,11 @@
package = AA8299FD2879BA0200FCA4BE /* XCRemoteSwiftPackageReference "Sauce" */;
productName = Sauce;
};
AAD54C712B4987AC00BF113E /* Settings */ = {
isa = XCSwiftPackageProductDependency;
package = AAD54C702B4987AC00BF113E /* XCRemoteSwiftPackageReference "Settings" */;
productName = Settings;
};
/* End XCSwiftPackageProductDependency section */
};
rootObject = AA40E16328695528000875C3 /* Project object */;
Expand Down
7 changes: 3 additions & 4 deletions MacCopier/About.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ class About {
)

private var links: NSMutableAttributedString {
let string = NSMutableAttributedString(string: "Website│GitHub│Contact",
let string = NSMutableAttributedString(string: "GitHub│Contact",
attributes: [NSAttributedString.Key.foregroundColor: NSColor.labelColor])
string.addAttribute(.link, value: "https://taohan.xyz/article/maccopier", range: NSRange(location: 0, length: 7))
string.addAttribute(.link, value: "https://github.com/DreamSaddle/MacCopier", range: NSRange(location: 8, length: 6))
string.addAttribute(.link, value: "mailto:1289747698@qq.com", range: NSRange(location: 15, length: 7))
string.addAttribute(.link, value: "https://github.com/DreamSaddle/MacCopier", range: NSRange(location: 0, length: 6))
string.addAttribute(.link, value: "mailto:1289747698@qq.com", range: NSRange(location: 7, length: 7))
return string
}

Expand Down
79 changes: 51 additions & 28 deletions MacCopier/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Cocoa
import SwiftUI
import SQLite3
import LaunchAtLogin
import Settings

class Message {
var messageDate: Int32
Expand All @@ -12,16 +13,25 @@ class Message {
}
}

struct Constants {
static let DEFAULT_MSG_MATCH_KEYWORDS = "码;code;認証番号;コード"
static let DEFAULT_MSG_CODE_MATCH_PATTERN = "\\b([0-9]{4,8})\\b"
}

// 扩展 UserDefaults 存储用户数据
extension UserDefaults {
public struct Keys {
static let autoPaste = "autoPaste"
static let hiddenStatusBar = "hiddenStatusBar"
static let msgMatchKeywords = "msgMatchKeywords"
static let msgCodeMatchPattern = "msgCodeMatchPattern"
}

struct ConfigValues {
static let autoPaste = false
static let hiddenStatusBar = false
static let msgMatchKeywords = Constants.DEFAULT_MSG_MATCH_KEYWORDS
static let msgCodeMatchPattern = Constants.DEFAULT_MSG_CODE_MATCH_PATTERN
}

@objc dynamic public var autoPaste: Bool {
Expand All @@ -32,22 +42,26 @@ extension UserDefaults {
get { bool(forKey: Keys.hiddenStatusBar) }
set { set(newValue, forKey: Keys.hiddenStatusBar) }
}
@objc dynamic public var msgMatchKeywords: String? {
get { string(forKey: Keys.msgMatchKeywords) }
set { set(newValue, forKey: Keys.msgMatchKeywords) }
}
@objc dynamic public var msgCodeMatchPattern: String? {
get { string(forKey: Keys.msgCodeMatchPattern) }
set { set(newValue, forKey: Keys.msgCodeMatchPattern) }
}
}

struct ClipboardController {
static let clipboard = Clipboard.init()
}


@main
class AppDelegate: NSObject, NSApplicationDelegate {

// 创建状态栏按钮
@IBOutlet weak var menu: NSMenu!
@IBOutlet weak var autoPasteMenuItem: NSMenuItem!
@IBOutlet weak var launchOnLoginMenuItem: NSMenuItem!
@IBOutlet weak var hiddenStatusBarMenuItem: NSMenuItem!


let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)

let codePattern = "([0-9]{4,8})"

// let codePattern = "\\b([0-9]{4,8})\\b"
var timer: Timer? = nil
let dbPath = "Messages/chat.db"
// 数据库连接
Expand All @@ -59,7 +73,18 @@ class AppDelegate: NSObject, NSApplicationDelegate {
// 可能就会存在我最后设置的是不显示, 再次启动此应用时先调用 applicationDidFinishLaunching 设置不显示
// 再执行 applicationDidBecomeActive 就又显示的情况
var visibleStatusBarCacheValue = false
let clipboard = Clipboard.init()

private lazy var settingsWindowController = SettingsWindowController(
panes: [
GeneralSettingViewController(),
RegexSettingViewController()
]
)

// 创建状态栏按钮
@IBOutlet weak var menu: NSMenu!
@IBOutlet weak var hiddenStatusBarMenuItem: NSMenuItem!
@IBOutlet weak var settingMenuItem : NSMenuItem!

private var fullDiskAccessAlert: NSAlert {
let alert = NSAlert()
Expand Down Expand Up @@ -89,8 +114,6 @@ class AppDelegate: NSObject, NSApplicationDelegate {
if let button = statusItem.button {
button.image = NSImage(named: "StatusIcon")
}
launchOnLoginMenuItem.state = LaunchAtLogin.isEnabled ? .on : .off
autoPasteMenuItem.state = UserDefaults.standard.autoPaste ? .on : .off
hiddenStatusBarMenuItem.state = UserDefaults.standard.hiddenStatusBar ? .on : .off
self.visibleStatusBarCacheValue = !UserDefaults.standard.hiddenStatusBar
self.changeStatusBarVisible(visible: !UserDefaults.standard.hiddenStatusBar)
Expand Down Expand Up @@ -124,21 +147,14 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
if db != nil {
sqlite3_finalize(db)
print("释放 sqlite 连接")
}
NSApplication.shared.terminate(self)
}

@IBAction func changeAutoPaste(_ sender: Any) {
if autoPasteMenuItem.state == .off && self.clipboard.checkAutoPastePermission() == false {
return
}
autoPasteMenuItem.state = autoPasteMenuItem.state == .on ? .off : .on
UserDefaults.standard.autoPaste = (autoPasteMenuItem.state == .on)
}

@IBAction func changeLaunchOnLogin(_ sender: Any) {
launchOnLoginMenuItem.state = launchOnLoginMenuItem.state == .on ? .off : .on
LaunchAtLogin.isEnabled = (launchOnLoginMenuItem.state == .on)
@IBAction func openSettingWindow(_ sender: Any) {
settingsWindowController.show()
settingsWindowController.window?.orderFrontRegardless()
}

@IBAction func openAboutWindow(_ sender: Any) {
Expand All @@ -160,15 +176,22 @@ class AppDelegate: NSObject, NSApplicationDelegate {
let message = getLatestMessage()
if message != nil {
let msgText = message!.text
if msgText.contains("code") || msgText.contains("") || msgText.contains("コード") || msgText.contains("認証番号") {
let msgMatchKeywords = UserDefaults.standard.msgMatchKeywords ?? Constants.DEFAULT_MSG_MATCH_KEYWORDS
var matched = false
for keyword in msgMatchKeywords.split(separator: ";").map({ String($0) }) {
matched = matched || msgText.contains(keyword)
}

if matched {
do {
let re = try NSRegularExpression(pattern: codePattern, options: [])
let pattern = UserDefaults.standard.msgCodeMatchPattern ?? Constants.DEFAULT_MSG_CODE_MATCH_PATTERN
let re = try NSRegularExpression(pattern: pattern, options: [])
let results = re.matches(in: msgText, range: NSRange(location: 0, length: msgText.count))
for result in results {
let code = (msgText as NSString).substring(with: result.range)
self.clipboard.copy(text: code)
ClipboardController.clipboard.copy(text: code)
if UserDefaults.standard.autoPaste {
self.clipboard.paste()
ClipboardController.clipboard.paste()
}

break
Expand Down
19 changes: 6 additions & 13 deletions MacCopier/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="19455" 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>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="19455"/>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="22154"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand Down Expand Up @@ -677,27 +678,19 @@
<customObject id="Voe-Tx-rLC" customClass="AppDelegate" customModule="MacCopier" customModuleProvider="target">
<connections>
<outlet property="a" destination="egg-s4-jCX" id="Ua5-xw-tgw"/>
<outlet property="autoPasteMenuItem" destination="ioc-Jz-R5r" id="rWY-g5-rjW"/>
<outlet property="hiddenStatusBarMenuItem" destination="mvW-w7-UAm" id="o0W-Zo-cD9"/>
<outlet property="launchOnLoginMenuItem" destination="iHu-8A-5st" id="2S8-Hb-Tq2"/>
<outlet property="menu" destination="ui6-lp-8e6" id="QJk-bD-iXv"/>
<outlet property="settingMenuItem" destination="Pn9-yc-bvo" id="Wzg-WN-8fS"/>
</connections>
</customObject>
<customObject id="YLy-65-1bz" customClass="NSFontManager"/>
<customObject id="Ady-hI-5gd" userLabel="First Responder" customClass="NSResponder" sceneMemberID="firstResponder"/>
<menu id="ui6-lp-8e6">
<items>
<menuItem title="自动粘贴" id="ioc-Jz-R5r">
<menuItem title="高级设置" id="Pn9-yc-bvo" userLabel="高级设置">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="changeAutoPaste:" target="Voe-Tx-rLC" id="fd6-SQ-XRI"/>
</connections>
</menuItem>
<menuItem title="登录时启动" id="iHu-8A-5st">
<attributedString key="attributedTitle"/>
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="changeLaunchOnLogin:" target="Voe-Tx-rLC" id="jDo-Jw-aZR"/>
<action selector="openSettingWindow:" target="Voe-Tx-rLC" id="aK3-VH-6j4"/>
</connections>
</menuItem>
<menuItem title="隐藏状态栏图标" id="mvW-w7-UAm">
Expand Down
53 changes: 53 additions & 0 deletions MacCopier/GeneralSettingViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import Cocoa
import Settings
import LaunchAtLogin


class GeneralSettingViewController: NSViewController, SettingsPane {
public let paneIdentifier = Settings.PaneIdentifier(rawValue: "preferences_general")
public let paneTitle = "Setting"
public let toolbarItemIcon = NSImage(named: NSImage.infoName)!

override var nibName: NSNib.Name? { "GeneralSettingViewController" }


@IBOutlet weak var launchAtLoginButton: NSButton!
@IBOutlet weak var autoPasteButton: NSButton!

override func viewDidLoad() {
super.viewDidLoad()
self.preferredContentSize = NSSize.init(width: 400, height: 300)
}

override func viewWillAppear() {
super.viewWillAppear()
populateLaunchAtLogin()
populateAutoPaste()
}



@IBAction func launchAtLoginChanged(_ sender: NSButton) {
LaunchAtLogin.isEnabled = (sender.state == .on)
}

@IBAction func autoPasteChanged(_ sender: NSButton) {
if sender.state == .on && ClipboardController.clipboard.checkAutoPastePermission() == false {
autoPasteButton.state = .off
UserDefaults.standard.autoPaste = false
return
}
autoPasteButton.state = sender.state
UserDefaults.standard.autoPaste = sender.state == .on
}

private func populateLaunchAtLogin() {
launchAtLoginButton.state = LaunchAtLogin.isEnabled ? .on : .off
}

private func populateAutoPaste() {
autoPasteButton.state = UserDefaults.standard.autoPaste ? .on : .off
}


}
Loading

0 comments on commit c4035c0

Please sign in to comment.