hammerspoon/jxa/openInChrome.jxa

41 lines
1.1 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

var args = $.NSProcessInfo.processInfo.arguments;
var argv = [];
var argc = args.count;
for (var i = 4; i < argc; i++) {
argv.push(ObjC.unwrap(args.objectAtIndex(i)));
}
var inputUrl = argv[0];
var Chrome = Application('Google Chrome');
var windows = Chrome.windows();
var foundTab = false;
// 遍历所有窗口和标签页
for (var i = 0; i < windows.length; i++) {
var window = windows[i];
var tabs = window.tabs();
for (var j = 0; j < tabs.length; j++) {
var tab = tabs[j];
if (tab.url().startsWith(inputUrl)) {
// 激活包含URL的窗口
Chrome.activate();
window.index = i + 1; // 窗口索引是从1开始的
// 激活对应的标签页
window.activeTabIndex = j + 1; // 标签索引也是从1开始的
foundTab = true;
break;
}
}
if (foundTab) {
break;
}
}
// 如果没有找到匹配的标签页打开新窗口并导航到URL
if (!foundTab) {
var newWindow = Chrome.Window().make();
var newTab = newWindow.tabs[0];
newTab.url = inputUrl;
Chrome.activate();
}