Last active
March 31, 2021 01:33
-
-
Save xyb994/0efc42d9369e206058058847db772dbd to your computer and use it in GitHub Desktop.
AppleScript for switching between en and zh-cn language on wikipedia in Safari
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set query_selector_en to "\"li.interlanguage-link.interwiki-en > a.interlanguage-link-target\"" | |
set query_selector_zh to "\"li.interlanguage-link.interwiki-zh > a.interlanguage-link-target\"" | |
set zh_replace_from to "\"/wiki\"" | |
set zh_replace_to to "\"/zh-cn\"" | |
set js_code_to_en to "window.location.href = document.querySelector(" & query_selector_en & ").href" | |
set js_code_to_zh to "window.location.href = document.querySelector(" & query_selector_zh & ").href.replace(" & zh_replace_from & ", " & zh_replace_to & ")" | |
tell application "Safari" | |
set current_url to URL of current tab of window 1 | |
if current_url contains "zh.wikipedia" then | |
set target_lang to "en" | |
else if current_url contains "en.wikipedia" then | |
set target_lang to "zh" | |
else | |
beep | |
display notification "The other language is not available" with title "Wikipedia EN-ZH Language Switcher" | |
return | |
end if | |
end tell | |
if target_lang is equal to "en" then | |
tell application "Safari" to do JavaScript js_code_to_en in document 1 | |
else if target_lang is equal to "zh" then | |
tell application "Safari" to do JavaScript js_code_to_zh in document 1 | |
end if |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment