Esempio n. 1
0
def show_active_tabs(args):
    brotab_logger.info('Showing active tabs: %s', args)
    apis = create_clients(args.target_hosts)
    for api in apis:
        tabs = api.get_active_tabs(args)
        for tab in tabs:
            print('%s\t%s' % (tab, api))
Esempio n. 2
0
def new_tab(args):
    prefix, window_id = parse_prefix_and_window_id(args.prefix_window_id)
    search_query = ' '.join(args.query)
    brotab_logger.info('Opening search for "%s", prefix "%s", window_id "%s"',
                search_query, prefix, window_id)
    url = "https://www.google.com/search?q=%s" % quote_plus(search_query)
    api = MultipleMediatorsAPI(create_clients(args.target_hosts))
    api.open_urls([url], prefix, window_id)
Esempio n. 3
0
def close_tabs(args):
    # Try stdin if arguments are empty
    tab_ids = args.tab_ids
    if len(args.tab_ids) == 0:
        tab_ids = split_tab_ids(read_stdin().strip())

    brotab_logger.info('Closing tabs: %s', tab_ids)
    api = MultipleMediatorsAPI(create_clients(args.target_hosts))
    tabs = api.close_tabs(tab_ids)
Esempio n. 4
0
def query_tabs(args):
    brotab_logger.info('Querying tabs: %s', args)
    d = vars(args)
    if d['info'] is not None:
        queryInfo = d['info']
    else:
        queryInfo = {k: v for k, v in d.items() if v is not None and k not in ['func', 'info']}
    api = MultipleMediatorsAPI(create_clients(args.target_hosts))
    for tab in api.query_tabs(queryInfo):
        print(tab)
Esempio n. 5
0
def list_tabs(args):
    """
    Use this to show duplicates:
        bt list | sort -k3 | uniq -f2 -D | cut -f1 | bt close
    """
    brotab_logger.info('Listing tabs')
    api = MultipleMediatorsAPI(create_clients(args.target_hosts))
    tabs = api.list_tabs([])
    message = '\n'.join(tabs) + '\n'
    sys.stdout.buffer.write(message.encode('utf8'))
Esempio n. 6
0
def get_words(args):
    # return tab.execute({javascript: "
    # [...new Set(document.body.innerText.match(/\w+/g))].sort().join('\n');
    # "})
    start = time.time()
    brotab_logger.info('Get words from tabs: %s, match_regex=%s, join_with=%s',
                args.tab_ids, args.match_regex, args.join_with)
    api = MultipleMediatorsAPI(create_clients(args.target_hosts))
    words = api.get_words(args.tab_ids, args.match_regex, args.join_with)
    print('\n'.join(words))
    delta = time.time() - start
Esempio n. 7
0
def create_clients(target_hosts=None) -> List[SingleMediatorAPI]:
    if target_hosts is None:
        ports = list(get_mediator_ports())
        hosts = ['localhost'] * len(ports)
    else:
        hosts, ports = parse_target_hosts(target_hosts)

    result = [SingleMediatorAPI(prefix, host=host, port=port)
              for prefix, host, port in zip(ascii_lowercase, hosts, ports)
              if is_port_accepting_connections(port, host)]
    brotab_logger.info('Created clients: %s', result)
    return result
Esempio n. 8
0
def open_urls(args):
    """
    curl -X POST 'http://*****:*****@urls.txt'
    curl -X POST 'http://*****:*****@urls.txt' -F 'window_id=749'

    where urls.txt containe one url per line (not JSON)
    """
    prefix, window_id = parse_prefix_and_window_id(args.prefix_window_id)
    urls = [line.strip() for line in sys.stdin.readlines()]
    brotab_logger.info('Opening URLs from stdin, prefix "%s", window_id "%s": %s',
                prefix, window_id, urls)
    api = MultipleMediatorsAPI(create_clients(args.target_hosts))
    api.open_urls(urls, prefix, window_id)
Esempio n. 9
0
def install_mediator(args):
    brotab_logger.info('Installing mediators')
    bt_mediator_path = shutil.which('bt_mediator')
    if is_windows():
        bt_mediator_path = make_windows_path_double_sep(bt_mediator_path)

    native_app_manifests = [
        ('mediator/firefox_mediator.json',
         '~/.mozilla/native-messaging-hosts/brotab_mediator.json'),
        ('mediator/chromium_mediator.json',
         '~/.config/chromium/NativeMessagingHosts/brotab_mediator.json'),
        ('mediator/chromium_mediator.json',
         '~/.config/google-chrome/NativeMessagingHosts/brotab_mediator.json'),
        ('mediator/chromium_mediator.json',
         '~/.config/BraveSoftware/Brave-Browser/NativeMessagingHosts/brotab_mediator.json'),
    ]

    if args.tests:
        native_app_manifests.append(
            ('mediator/chromium_mediator_tests.json',
             '~/.config/chromium/NativeMessagingHosts/brotab_mediator.json'),
        )

    from pkg_resources import resource_string
    for filename, destination in native_app_manifests:
        destination = os.path.expanduser(os.path.expandvars(destination))
        template = resource_string(__name__, filename).decode('utf8')
        manifest = template.replace(r'$PWD/brotab_mediator.py', bt_mediator_path)
        brotab_logger.info('Installing template %s into %s', filename, destination)
        print('Installing mediator manifest %s' % destination)

        os.makedirs(os.path.dirname(destination), exist_ok=True)
        with open(destination, 'w') as file_:
            file_.write(manifest)

        if is_windows() and 'mozilla' in destination:
            register_native_manifest_windows_firefox(destination)
        if is_windows() and 'chrome' in destination:
            register_native_manifest_windows_chrome(destination)
        if is_windows() and 'Brave' in destination:
            register_native_manifest_windows_brave(destination)

    print('Link to Firefox extension: https://addons.mozilla.org/en-US/firefox/addon/brotab/')
    print(
        'Link to Chrome (Chromium)/Brave extension: https://chrome.google.com/webstore/detail/brotab/mhpeahbikehnfkfnmopaigggliclhmnc/')
Esempio n. 10
0
def index_tabs(args):
    if args.tsv is None:
        args.tsv = in_temp_dir('tabs.tsv')
        args.cleanup = True
        brotab_logger.info(
            'index_tabs: retrieving tabs from browser into file %s', args.tsv)
        start = time.time()
        get_text(args)
        delta = time.time() - start
        brotab_logger.info('getting text took %s', delta)

    start = time.time()
    index(args.sqlite, args.tsv)
    delta = time.time() - start
    brotab_logger.info('sqlite create took %s, size %s',
                       delta, get_file_size(args.sqlite))
Esempio n. 11
0
def show_clients(args):
    brotab_logger.info('Showing clients')
    for client in create_clients(args.target_hosts):
        print(client)
Esempio n. 12
0
def show_windows(args):
    brotab_logger.info('Showing windows')
    api = MultipleMediatorsAPI(create_clients(args.target_hosts))
    tabs = api.list_tabs([])
    _print_available_windows(tabs)
Esempio n. 13
0
def get_html(args):
    brotab_logger.info('Get html from tabs')
    api = MultipleMediatorsAPI(create_clients(args.target_hosts))
    return get_text_or_html(api.get_html, args)
Esempio n. 14
0
def activate_tab(args):
    brotab_logger.info('Activating tab: %s', args.tab_id)
    api = MultipleMediatorsAPI(create_clients(args.target_hosts))
    api.activate_tab(args.tab_id, args.focused)
Esempio n. 15
0
def move_tabs(args):
    brotab_logger.info('Moving tabs')
    api = MultipleMediatorsAPI(create_clients(args.target_hosts))
    api.move_tabs([])