-
Notifications
You must be signed in to change notification settings - Fork 116
Safari and Chrome
Tatsh edited this page May 6, 2020
·
6 revisions
← previous
· · · · · · · · · · · · · ·
next →
var safari = Application('Safari')
safari.doJavaScript('alert("Hello!")', { in: safari.windows[0].currentTab })
You must allow JavaScript from Apple Events under View > Developer > Allow JavaScript from Apple Events.
const chrome = Application('Google Chrome')
const currentTab = chrome.windows[0].activeTab()
currentTab.execute({javascript: 'alert("Hello")'})
ObjC.import('stdlib')
const t = Application('Google Chrome')
.windows[0]
.activeTab()
t.url = 'chrome://net-internals/#dns'
delay(0.5)
t.execute({
javascript: 'document.querySelector("#dns-view-clear-cache").click()'
})
delay(0.5)
$.exit(0)
function run() {
var appSafari = Application('Safari'),
winFront = appSafari.windows[0],
lstTabs = winFront.tabs,
iTab = lstTabs.push(new appSafari.Tab())-1,
oTab = lstTabs[iTab];
return oTab;
}
// Application("Safari").windows.at(0).tabs.at(4)
var safari = Application('Safari');
/* Returns the first tab that contains a page whose URL starts with the provided URL */
function getTab(url, window) {
var result;
window = window || safari.windows[0];
for (var index in window.tabs) {
var tab = window.tabs[index];
var tabURL = tab.url();
if (tabURL && tabURL.startsWith(url)) {
result = tab;
break;
}
}
return result;
}
var tab = getTab("https://www.apple.com/");
var fullURL = tab.url();
var tab = getTab("https://www.apple.com/");
tab.url = "https://www.icloud.com/";
var tab = getTab("https://www.apple.com/");
safari.windows[0].currentTab = tab;
let app = Application.currentApplication();
app.includeStandardAdditions = true;
let se = Application('System Events');
function clickMenuItem(processName, menuName, menuItemNames, interval) {
interval = interval || 0.5;
let process = se.processes.byName(processName);
let menu = process.menuBars[0].menuBarItems.byName(menuName);
menu.click();
delay(interval);
let previous = menu;
for (let menuItemName of menuItemNames) {
let menuItem = previous.menus[0].menuItems.byName(menuItemName);
menuItem.click();
delay(interval);
previous = menuItem;
}
return previous;
}
let safari = Application('Safari');
safari.activate();
delay(1.0);
clickMenuItem('Safari', 'Develop', ['User Agent', 'Safari — iOS 12.1.3 — iPad']);
← previous
· · · · · · · · · · · · · ·
next →
- Foreword
- Conventions Used in This Cookbook
- Using JavaScript for Automation
- ES6 Features in JXA
- Getting the Application Instance
- User Interactions
- User Interaction with Files and Folders
- Using Objective-C (ObjC) with JXA
- Shell and CLI Interactions
- Importing Scripts
- iTunes
- Keynote
- Messages
- System Events
- Safari & Chrome
- Script Editor
- XML
- Examples