Skip to content

Instantly share code, notes, and snippets.

@PickledChair
Last active June 3, 2019 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PickledChair/f5a5bca53f1b2d4ec6f111f595d629a5 to your computer and use it in GitHub Desktop.
Save PickledChair/f5a5bca53f1b2d4ec6f111f595d629a5 to your computer and use it in GitHub Desktop.
The script for switching two WebViews on Pythonista 3.
import ui
import console
import sys
user_action = True
class MyWebViewDelegate(object):
def webview_should_start_load(self, webview, url, nav_type):
global user_action
if nav_type == 'link_clicked' or user_action:
textfield = webview.superview['textfield1']
textfield.text = url
user_action = False
return True
def webview_did_start_load(self, webview):
pass
def webview_did_finish_load(self, webview):
pass
def webview_did_fail_load(self, webview, error_code, error_msg):
pass
def page_back(sender):
global user_action
user_action = True
webview = sender.superview['webview1']
webview.go_back()
def page_forward(sender):
global user_action
user_action = True
webview = sender.superview['webview1']
webview.go_forward()
def jump_to_link(sender):
global user_action
user_action = True
textfield = sender.superview['textfield1']
if textfield.text:
webview = sender.superview['webview1']
webview.load_url(textfield.text)
def switch_webview(sender):
idx = sender.selected_index
webview1 = sender.superview['webview1']
webview2 = sender.superview['webview2']
if idx == 0:
webview2.alpha = 0.0
webview1.alpha = 1.0
webview1.bring_to_front()
else:
webview1.alpha = 0.0
webview2.alpha = 1.0
webview2.bring_to_front()
v = ui.load_view('translate_set')
textfield = v['textfield1']
textfield.clear_button_mode = 'unless_editing'
webview1 = v['webview1']
webview1.delegate = MyWebViewDelegate()
text = ''
if len(sys.argv) > 1:
text = sys.argv[1]
if text.startswith('http://') or text.startswith('https://'):
result = console.alert('URLを開く', '取得済みのURLがあります。開きますか?', 'はい', 'Googleを開く', 'Pythonista Docs を開く')
if result == 2:
webview1.load_url('https://www.google.co.jp/')
textfield.text = 'https://www.google.co.jp/'
elif result == 3:
webview1.load_url('http://omz-software.com/pythonista/docs/')
textfield.text = 'http://omz-software.com/pythonista/docs/'
else:
webview1.load_url(text)
textfield.text = text
else:
result = console.alert('URLを選択', 'どちらを開きますか?', 'Googleを開く', 'Pythonista Docs を開く')
if result == 1:
webview1.load_url('https://www.google.co.jp/')
textfield.text = 'https://www.google.co.jp/'
else:
webview1.load_url('http://omz-software.com/pythonista/docs/')
textfield.text = 'http://omz-software.com/pythonista/docs/'
webview2 = v['webview2']
webview2.load_url('https://translate.google.co.jp/?hl=ja&tab=wT')
webview2.alpha = 0.0
webview1.bring_to_front()
v.present('sheet')
[
{
"nodes" : [
{
"nodes" : [
],
"frame" : "{{6, 6}, {151, 32}}",
"class" : "TextField",
"attributes" : {
"uuid" : "97EF78A8-7CC4-4E0B-B95D-934CEB909B49",
"flex" : "W",
"frame" : "{{20, 104}, {200, 32}}",
"border_color" : "RGBA(0.593750,0.825893,1.000000,1.000000)",
"alignment" : "left",
"autocorrection_type" : "default",
"placeholder" : "input some URL",
"font_name" : "<System>",
"spellchecking_type" : "default",
"class" : "TextField",
"name" : "textfield1",
"font_size" : 17
},
"selected" : false
},
{
"nodes" : [
],
"frame" : "{{165, 6}, {32, 32}}",
"class" : "Button",
"attributes" : {
"flex" : "L",
"action" : "jump_to_link",
"font_size" : 15,
"frame" : "{{80, 104}, {80, 32}}",
"title" : "",
"uuid" : "41B10096-4A2B-406B-B7CE-9EF4CA0FA54D",
"class" : "Button",
"name" : "button1",
"image_name" : "iob:ios7_search_strong_32"
},
"selected" : false
},
{
"nodes" : [
],
"frame" : "{{198, 6}, {32, 32}}",
"class" : "Button",
"attributes" : {
"flex" : "L",
"action" : "page_back",
"font_size" : 15,
"frame" : "{{80, 104}, {80, 32}}",
"title" : "",
"uuid" : "E73EF2B7-D590-45AA-B26B-F0C51577F19F",
"class" : "Button",
"name" : "button2",
"image_name" : "iob:ios7_arrow_left_32"
},
"selected" : false
},
{
"nodes" : [
],
"frame" : "{{231, 6}, {32, 32}}",
"class" : "Button",
"attributes" : {
"flex" : "L",
"action" : "page_forward",
"font_size" : 15,
"frame" : "{{80, 104}, {80, 32}}",
"title" : "",
"uuid" : "E44E9F06-9F00-45B7-B286-9DA6285A21E9",
"class" : "Button",
"name" : "button3",
"image_name" : "iob:ios7_arrow_right_32"
},
"selected" : false
},
{
"nodes" : [
],
"frame" : "{{0, 77}, {263, 263}}",
"class" : "WebView",
"attributes" : {
"scales_to_fit" : true,
"frame" : "{{20, 20}, {200, 200}}",
"flex" : "WH",
"class" : "WebView",
"name" : "webview1",
"uuid" : "E3DAFEDA-C701-49DC-9344-038DA741FEE9"
},
"selected" : false
},
{
"nodes" : [
],
"frame" : "{{46, 43}, {172, 29}}",
"class" : "SegmentedControl",
"attributes" : {
"action" : "switch_webview",
"flex" : "LR",
"segments" : "サイトビュー|Google翻訳",
"frame" : "{{68, 280}, {120, 29}}",
"class" : "SegmentedControl",
"uuid" : "3FA1B71E-C34E-46B0-8EFD-236DFF7F774C",
"name" : "segmentedcontrol1"
},
"selected" : false
},
{
"nodes" : [
],
"frame" : "{{0, 77}, {263, 263}}",
"class" : "WebView",
"attributes" : {
"uuid" : "92B073BF-5420-4248-AA00-45613A67ED82",
"frame" : "{{28, 194}, {200, 200}}",
"scales_to_fit" : true,
"class" : "WebView",
"name" : "webview2",
"flex" : "WH"
},
"selected" : false
}
],
"frame" : "{{0, 0}, {263, 340}}",
"class" : "View",
"attributes" : {
"background_color" : "RGBA(1.000000,1.000000,1.000000,1.000000)",
"enabled" : true,
"tint_color" : "RGBA(0.000000,0.478000,1.000000,1.000000)",
"name" : "Google翻訳切替ビューア",
"border_color" : "RGBA(0.000000,0.000000,0.000000,1.000000)",
"flex" : ""
},
"selected" : false
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment