forked from manulaiko/ulauncher-openInBrowser
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
47 lines (35 loc) · 1.57 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from ulauncher.api.client.Extension import Extension
from ulauncher.api.shared.action.ExtensionCustomAction import ExtensionCustomAction
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.shared.event import KeywordQueryEvent, ItemEnterEvent
from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
from ulauncher.api.shared.action.HideWindowAction import HideWindowAction
import webbrowser
import re
class OpenInBrowser(Extension):
def __init__(self):
super(OpenInBrowser, self).__init__()
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
self.subscribe(ItemEnterEvent, ItemEnterEventListener())
class KeywordQueryEventListener(EventListener):
def on_event(self, event, extension):
data = event.get_argument()
items = [
ExtensionResultItem(
icon='images/icon.png',
name=event.get_argument(),
description='Open "%s" in the browser' % event.get_argument(),
on_enter=ExtensionCustomAction(data, keep_app_open=True)
)
]
return RenderResultListAction(items)
class ItemEnterEventListener(EventListener):
def on_event(self, event, extension):
data = event.get_data()
if not re.match(r'^https?://', data):
data = 'https://'+ data
webbrowser.open_new_tab(data)
return RenderResultListAction([])
if __name__ == '__main__':
OpenInBrowser().run()