在已有 app_options 的基础上提供根据当前浏览器页面进行条件判断的功能
Nobody has claimed this yet.
- Dominant language
- Swift
- Stars
- 6.4k
- Forks
- 553
- PR merge metrics
- No merged PRs in 30d
Description
已经验证了通过类似 AppleScript 的进程间通信来实现的思路,不需要安装附加插件(除了 Firefox,后面会说明原因)
最简单的方法当然是直接调用 AppleScript 解释器:
(* 理论上适用于任何 Chromium-Based *)
tell application "Arc" to return URL of active tab of front window
(* Safari *)
tell application "Safari" to return URL of current tab of front window
Foundation 中已有 NSAppleScript,调用即可(但我没有找到“在切换 tab 时触发”等事件监听,所以可能需要轮询
也可以通过 ScriptingBridge 直接用 Objective-C 写逻辑:
#import <Foundation/Foundation.h>
#import "browser.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
BrowserApplication *browser = [SBApplication applicationWithBundleIdentifier:@"company.thebrowser.Browser"];
// BrowserApplication *browser = [SBApplication applicationWithBundleIdentifier:@"com.apple.safari"];
// for (BrowserWindow *window in [browser windows]) {
// NSLog(@"window %ld: name is %@", [window index], [window name]);
// }
BrowserWindow *window = browser.windows.firstObject;
// Chromium-Based 只有 activeTab,Safari 只有 currentTab,以此简单区分
BrowserTab *tab = [window respondsToSelector:@selector(activeTab)] ? window.activeTab : window.currentTab;
NSLog(@"active tab is %@ %@", tab.name, tab.URL);
}
return 0;
}
这个 browser.h 是我根据 Arc 和 Safari 原始导出的 protocol 修改合并而来的,导出 protocol 的命令是:
# 两个 CLI 都随 Xcode 安装
sdef /Applications/Arc.app/ > Firefox.sdef
sdp -fh -o Arc.h --basename 'Browser' Arc.sdef
如果有能避免维护一个 protocol 头文件的方法是否会更好?我没写过 Objective-C,网上搜索到的 id 类型似乎仍然需要“某处”有该方法约定存在,而不能完全省略 protocol 声明
Firefox 的问题是它的文档表明它需要从 document 属性获取 path 信息,但是从下面这张截图可以看到这个功能并没有做出来:
从 bugzilla 页面 来看这个问题貌似从 Firefox 3 开始有人注意到现在都没人真正弄出来……这个页面靠后的帖子提到一个 workaround 是用 插件 将 URL(默认是只有域名部分,不过对匹配网站来讲应该完全足够)放到窗口标题。AppleScript 是能正常获取到窗口标题的,因此最后就能获得当前网站。
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the app_options entry point and review the NSAppleScript and ScriptingBridge approaches described in the issue, including the generated browser.h and Arc.sdef files. Compare browser support, especially Firefox's window-title workaround, and define a supported way to condition behavior on the current page before implementation. Done means the feature works without an additional plugin where possible and its browser limitations are documented.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- objective-c
- Domain
- desktop
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100