mirror of
				https://github.com/dgtlmoon/changedetection.io.git
				synced 2025-11-04 00:27:48 +00:00 
			
		
		
		
	Compare commits
	
		
			21 Commits
		
	
	
		
			browserste
			...
			1448-basic
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					719d24ad23 | ||
| 
						 | 
					7ded35a3ac | ||
| 
						 | 
					6ace90a8ea | ||
| 
						 | 
					6663768396 | ||
| 
						 | 
					284d0a0246 | ||
| 
						 | 
					04429b88ee | ||
| 
						 | 
					d11c581089 | ||
| 
						 | 
					5e5fe6dc05 | ||
| 
						 | 
					46ab2846d0 | ||
| 
						 | 
					143971123d | ||
| 
						 | 
					04d2d3fb00 | ||
| 
						 | 
					236f0c098d | ||
| 
						 | 
					582c6b465b | ||
| 
						 | 
					a021ba87fa | ||
| 
						 | 
					e9057cb851 | ||
| 
						 | 
					72ec438caa | ||
| 
						 | 
					367dec48e1 | ||
| 
						 | 
					dd87912c88 | ||
| 
						 | 
					0126cb0aac | ||
| 
						 | 
					463b2d0449 | ||
| 
						 | 
					e4f6d54ae2 | 
@@ -38,7 +38,7 @@ from flask_paginate import Pagination, get_page_parameter
 | 
			
		||||
from changedetectionio import html_tools
 | 
			
		||||
from changedetectionio.api import api_v1
 | 
			
		||||
 | 
			
		||||
__version__ = '0.41.1'
 | 
			
		||||
__version__ = '0.42.1'
 | 
			
		||||
 | 
			
		||||
datastore = None
 | 
			
		||||
 | 
			
		||||
@@ -403,26 +403,35 @@ def changedetection_app(config=None, datastore_o=None):
 | 
			
		||||
 | 
			
		||||
        # Sort by last_changed and add the uuid which is usually the key..
 | 
			
		||||
        sorted_watches = []
 | 
			
		||||
        search_q = request.args.get('q').strip().lower() if request.args.get('q') else False
 | 
			
		||||
        for uuid, watch in datastore.data['watching'].items():
 | 
			
		||||
 | 
			
		||||
            if limit_tag != None:
 | 
			
		||||
            if limit_tag:
 | 
			
		||||
                # Support for comma separated list of tags.
 | 
			
		||||
                if watch['tag'] is None:
 | 
			
		||||
                if not watch.get('tag'):
 | 
			
		||||
                    continue
 | 
			
		||||
                for tag_in_watch in watch['tag'].split(','):
 | 
			
		||||
                for tag_in_watch in watch.get('tag', '').split(','):
 | 
			
		||||
                    tag_in_watch = tag_in_watch.strip()
 | 
			
		||||
                    if tag_in_watch == limit_tag:
 | 
			
		||||
                        watch['uuid'] = uuid
 | 
			
		||||
                        sorted_watches.append(watch)
 | 
			
		||||
                        if search_q:
 | 
			
		||||
                            if (watch.get('title') and search_q in watch.get('title').lower()) or search_q in watch.get('url', '').lower():
 | 
			
		||||
                                sorted_watches.append(watch)
 | 
			
		||||
                        else:
 | 
			
		||||
                            sorted_watches.append(watch)
 | 
			
		||||
 | 
			
		||||
            else:
 | 
			
		||||
                watch['uuid'] = uuid
 | 
			
		||||
                sorted_watches.append(watch)
 | 
			
		||||
                #watch['uuid'] = uuid
 | 
			
		||||
                if search_q:
 | 
			
		||||
                    if (watch.get('title') and search_q in watch.get('title').lower()) or search_q in watch.get('url', '').lower():
 | 
			
		||||
                        sorted_watches.append(watch)
 | 
			
		||||
                else:
 | 
			
		||||
                    sorted_watches.append(watch)
 | 
			
		||||
 | 
			
		||||
        existing_tags = datastore.get_all_tags()
 | 
			
		||||
        form = forms.quickWatchForm(request.form)
 | 
			
		||||
        page = request.args.get(get_page_parameter(), type=int, default=1)
 | 
			
		||||
        total_count = len(sorted_watches) if sorted_watches else len(datastore.data['watching'])
 | 
			
		||||
        total_count = len(sorted_watches)
 | 
			
		||||
        pagination = Pagination(page=page, total=total_count, per_page=int(os.getenv('pagination_per_page', 50)), css_framework = "semantic")
 | 
			
		||||
 | 
			
		||||
        output = render_template(
 | 
			
		||||
@@ -437,6 +446,7 @@ def changedetection_app(config=None, datastore_o=None):
 | 
			
		||||
                                 hosted_sticky=os.getenv("SALTED_PASS", False) == False,
 | 
			
		||||
                                 pagination=pagination,
 | 
			
		||||
                                 queued_uuids=[q_uuid.item['uuid'] for q_uuid in update_q.queue],
 | 
			
		||||
                                 search_q=request.args.get('q','').strip(),
 | 
			
		||||
                                 sort_attribute=request.args.get('sort') if request.args.get('sort') else request.cookies.get('sort'),
 | 
			
		||||
                                 sort_order=request.args.get('order') if request.args.get('order') else request.cookies.get('order'),
 | 
			
		||||
                                 system_default_fetcher=datastore.data['settings']['application'].get('fetch_backend'),
 | 
			
		||||
@@ -690,6 +700,7 @@ def changedetection_app(config=None, datastore_o=None):
 | 
			
		||||
                                     form=form,
 | 
			
		||||
                                     has_default_notification_urls=True if len(datastore.data['settings']['application']['notification_urls']) else False,
 | 
			
		||||
                                     has_empty_checktime=using_default_check_time,
 | 
			
		||||
                                     has_extra_headers_file=watch.has_extra_headers_file or datastore.has_extra_headers_file,
 | 
			
		||||
                                     is_html_webdriver=is_html_webdriver,
 | 
			
		||||
                                     jq_support=jq_support,
 | 
			
		||||
                                     playwright_enabled=os.getenv('PLAYWRIGHT_DRIVER_URL', False),
 | 
			
		||||
@@ -1434,6 +1445,7 @@ def check_for_new_version():
 | 
			
		||||
        # Check daily
 | 
			
		||||
        app.config.exit.wait(86400)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def notification_runner():
 | 
			
		||||
    global notification_debug_log
 | 
			
		||||
    from datetime import datetime
 | 
			
		||||
 
 | 
			
		||||
@@ -27,58 +27,106 @@ import os
 | 
			
		||||
import logging
 | 
			
		||||
from changedetectionio.store import ChangeDetectionStore
 | 
			
		||||
from changedetectionio import login_optionally_required
 | 
			
		||||
browsersteps_live_ui_o = {}
 | 
			
		||||
browsersteps_playwright_browser_interface = None
 | 
			
		||||
browsersteps_playwright_browser_interface_browser = None
 | 
			
		||||
browsersteps_playwright_browser_interface_context = None
 | 
			
		||||
browsersteps_playwright_browser_interface_end_time = None
 | 
			
		||||
browsersteps_playwright_browser_interface_start_time = None
 | 
			
		||||
 | 
			
		||||
def cleanup_playwright_session():
 | 
			
		||||
browsersteps_sessions = {}
 | 
			
		||||
io_interface_context = None
 | 
			
		||||
 | 
			
		||||
    global browsersteps_live_ui_o
 | 
			
		||||
    global browsersteps_playwright_browser_interface
 | 
			
		||||
    global browsersteps_playwright_browser_interface_browser
 | 
			
		||||
    global browsersteps_playwright_browser_interface_context
 | 
			
		||||
    global browsersteps_playwright_browser_interface_end_time
 | 
			
		||||
    global browsersteps_playwright_browser_interface_start_time
 | 
			
		||||
 | 
			
		||||
    browsersteps_live_ui_o = {}
 | 
			
		||||
    browsersteps_playwright_browser_interface = None
 | 
			
		||||
    browsersteps_playwright_browser_interface_browser = None
 | 
			
		||||
    browsersteps_playwright_browser_interface_end_time = None
 | 
			
		||||
    browsersteps_playwright_browser_interface_start_time = None
 | 
			
		||||
 | 
			
		||||
    print("Cleaning up old playwright session because time was up, calling .goodbye()")
 | 
			
		||||
    try:
 | 
			
		||||
        browsersteps_playwright_browser_interface_context.goodbye()
 | 
			
		||||
    except Exception as e:
 | 
			
		||||
        print ("Got exception in shutdown, probably OK")
 | 
			
		||||
        print (str(e))
 | 
			
		||||
 | 
			
		||||
    browsersteps_playwright_browser_interface_context = None
 | 
			
		||||
 | 
			
		||||
    print ("Cleaning up old playwright session because time was up - done")
 | 
			
		||||
 | 
			
		||||
def construct_blueprint(datastore: ChangeDetectionStore):
 | 
			
		||||
 | 
			
		||||
    browser_steps_blueprint = Blueprint('browser_steps', __name__, template_folder="templates")
 | 
			
		||||
 | 
			
		||||
    def start_browsersteps_session(watch_uuid):
 | 
			
		||||
        from . import nonContext
 | 
			
		||||
        from . import browser_steps
 | 
			
		||||
        import time
 | 
			
		||||
        global browsersteps_sessions
 | 
			
		||||
        global io_interface_context
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        # We keep the playwright session open for many minutes
 | 
			
		||||
        seconds_keepalive = int(os.getenv('BROWSERSTEPS_MINUTES_KEEPALIVE', 10)) * 60
 | 
			
		||||
 | 
			
		||||
        browsersteps_start_session = {'start_time': time.time()}
 | 
			
		||||
 | 
			
		||||
        # You can only have one of these running
 | 
			
		||||
        # This should be very fine to leave running for the life of the application
 | 
			
		||||
        # @idea - Make it global so the pool of watch fetchers can use it also
 | 
			
		||||
        if not io_interface_context:
 | 
			
		||||
            io_interface_context = nonContext.c_sync_playwright()
 | 
			
		||||
            # Start the Playwright context, which is actually a nodejs sub-process and communicates over STDIN/STDOUT pipes
 | 
			
		||||
            io_interface_context = io_interface_context.start()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        # keep it alive for 10 seconds more than we advertise, sometimes it helps to keep it shutting down cleanly
 | 
			
		||||
        keepalive = "&timeout={}".format(((seconds_keepalive + 3) * 1000))
 | 
			
		||||
        try:
 | 
			
		||||
            browsersteps_start_session['browser'] = io_interface_context.chromium.connect_over_cdp(
 | 
			
		||||
                os.getenv('PLAYWRIGHT_DRIVER_URL', '') + keepalive)
 | 
			
		||||
        except Exception as e:
 | 
			
		||||
            if 'ECONNREFUSED' in str(e):
 | 
			
		||||
                return make_response('Unable to start the Playwright Browser session, is it running?', 401)
 | 
			
		||||
            else:
 | 
			
		||||
                return make_response(str(e), 401)
 | 
			
		||||
 | 
			
		||||
        proxy_id = datastore.get_preferred_proxy_for_watch(uuid=watch_uuid)
 | 
			
		||||
        proxy = None
 | 
			
		||||
        if proxy_id:
 | 
			
		||||
            proxy_url = datastore.proxy_list.get(proxy_id).get('url')
 | 
			
		||||
            if proxy_url:
 | 
			
		||||
 | 
			
		||||
                # Playwright needs separate username and password values
 | 
			
		||||
                from urllib.parse import urlparse
 | 
			
		||||
                parsed = urlparse(proxy_url)
 | 
			
		||||
                proxy = {'server': proxy_url}
 | 
			
		||||
 | 
			
		||||
                if parsed.username:
 | 
			
		||||
                    proxy['username'] = parsed.username
 | 
			
		||||
 | 
			
		||||
                if parsed.password:
 | 
			
		||||
                    proxy['password'] = parsed.password
 | 
			
		||||
 | 
			
		||||
                print("Browser Steps: UUID {} selected proxy {}".format(watch_uuid, proxy_url))
 | 
			
		||||
 | 
			
		||||
        # Tell Playwright to connect to Chrome and setup a new session via our stepper interface
 | 
			
		||||
        browsersteps_start_session['browserstepper'] = browser_steps.browsersteps_live_ui(
 | 
			
		||||
            playwright_browser=browsersteps_start_session['browser'],
 | 
			
		||||
            proxy=proxy)
 | 
			
		||||
 | 
			
		||||
        # For test
 | 
			
		||||
        #browsersteps_start_session['browserstepper'].action_goto_url(value="http://example.com?time="+str(time.time()))
 | 
			
		||||
 | 
			
		||||
        return browsersteps_start_session
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    @login_optionally_required
 | 
			
		||||
    @browser_steps_blueprint.route("/browsersteps_update", methods=['GET', 'POST'])
 | 
			
		||||
    @browser_steps_blueprint.route("/browsersteps_start_session", methods=['GET'])
 | 
			
		||||
    def browsersteps_start_session():
 | 
			
		||||
        # A new session was requested, return sessionID
 | 
			
		||||
 | 
			
		||||
        import uuid
 | 
			
		||||
        global browsersteps_sessions
 | 
			
		||||
 | 
			
		||||
        browsersteps_session_id = str(uuid.uuid4())
 | 
			
		||||
        watch_uuid = request.args.get('uuid')
 | 
			
		||||
 | 
			
		||||
        if not watch_uuid:
 | 
			
		||||
            return make_response('No Watch UUID specified', 500)
 | 
			
		||||
 | 
			
		||||
        print("Starting connection with playwright")
 | 
			
		||||
        logging.debug("browser_steps.py connecting")
 | 
			
		||||
        browsersteps_sessions[browsersteps_session_id] = start_browsersteps_session(watch_uuid)
 | 
			
		||||
        print("Starting connection with playwright - done")
 | 
			
		||||
        return {'browsersteps_session_id': browsersteps_session_id}
 | 
			
		||||
 | 
			
		||||
    # A request for an action was received
 | 
			
		||||
    @login_optionally_required
 | 
			
		||||
    @browser_steps_blueprint.route("/browsersteps_update", methods=['POST'])
 | 
			
		||||
    def browsersteps_ui_update():
 | 
			
		||||
        import base64
 | 
			
		||||
        import playwright._impl._api_types
 | 
			
		||||
        import time
 | 
			
		||||
 | 
			
		||||
        global browsersteps_sessions
 | 
			
		||||
        from changedetectionio.blueprint.browser_steps import browser_steps
 | 
			
		||||
 | 
			
		||||
        global browsersteps_live_ui_o, browsersteps_playwright_browser_interface_end_time
 | 
			
		||||
        global browsersteps_playwright_browser_interface_browser
 | 
			
		||||
        global browsersteps_playwright_browser_interface
 | 
			
		||||
        global browsersteps_playwright_browser_interface_start_time
 | 
			
		||||
 | 
			
		||||
        step_n = None
 | 
			
		||||
        remaining =0
 | 
			
		||||
        uuid = request.args.get('uuid')
 | 
			
		||||
 | 
			
		||||
@@ -87,13 +135,9 @@ def construct_blueprint(datastore: ChangeDetectionStore):
 | 
			
		||||
        if not browsersteps_session_id:
 | 
			
		||||
            return make_response('No browsersteps_session_id specified', 500)
 | 
			
		||||
 | 
			
		||||
        # Because we don't "really" run in a context manager ( we make the playwright interface global/long-living )
 | 
			
		||||
        # We need to manage the shutdown when the time is up
 | 
			
		||||
        if browsersteps_playwright_browser_interface_end_time:
 | 
			
		||||
            remaining = browsersteps_playwright_browser_interface_end_time-time.time()
 | 
			
		||||
            if browsersteps_playwright_browser_interface_end_time and remaining <= 0:
 | 
			
		||||
                cleanup_playwright_session()
 | 
			
		||||
                return make_response('Browser session expired, please reload the Browser Steps interface', 401)
 | 
			
		||||
        if not browsersteps_sessions.get(browsersteps_session_id):
 | 
			
		||||
            return make_response('No session exists under that ID', 500)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        # Actions - step/apply/etc, do the thing and return state
 | 
			
		||||
        if request.method == 'POST':
 | 
			
		||||
@@ -112,12 +156,7 @@ def construct_blueprint(datastore: ChangeDetectionStore):
 | 
			
		||||
            # @todo try.. accept.. nice errors not popups..
 | 
			
		||||
            try:
 | 
			
		||||
 | 
			
		||||
                this_session = browsersteps_live_ui_o.get(browsersteps_session_id)
 | 
			
		||||
                if not this_session:
 | 
			
		||||
                    print("Browser exited")
 | 
			
		||||
                    return make_response('Browser session ran out of time :( Please reload this page.', 401)
 | 
			
		||||
 | 
			
		||||
                this_session.call_action(action_name=step_operation,
 | 
			
		||||
                browsersteps_sessions[browsersteps_session_id]['browserstepper'].call_action(action_name=step_operation,
 | 
			
		||||
                                         selector=step_selector,
 | 
			
		||||
                                         optional_value=step_optional_value)
 | 
			
		||||
 | 
			
		||||
@@ -129,108 +168,43 @@ def construct_blueprint(datastore: ChangeDetectionStore):
 | 
			
		||||
            # Get visual selector ready/update its data (also use the current filter info from the page?)
 | 
			
		||||
            # When the last 'apply' button was pressed
 | 
			
		||||
            # @todo this adds overhead because the xpath selection is happening twice
 | 
			
		||||
            u = this_session.page.url
 | 
			
		||||
            u = browsersteps_sessions[browsersteps_session_id]['browserstepper'].page.url
 | 
			
		||||
            if is_last_step and u:
 | 
			
		||||
                (screenshot, xpath_data) = this_session.request_visualselector_data()
 | 
			
		||||
                (screenshot, xpath_data) = browsersteps_sessions[browsersteps_session_id]['browserstepper'].request_visualselector_data()
 | 
			
		||||
                datastore.save_screenshot(watch_uuid=uuid, screenshot=screenshot)
 | 
			
		||||
                datastore.save_xpath_data(watch_uuid=uuid, data=xpath_data)
 | 
			
		||||
 | 
			
		||||
        # Setup interface
 | 
			
		||||
        if request.method == 'GET':
 | 
			
		||||
#        if not this_session.page:
 | 
			
		||||
#            cleanup_playwright_session()
 | 
			
		||||
#            return make_response('Browser session ran out of time :( Please reload this page.', 401)
 | 
			
		||||
 | 
			
		||||
            if not browsersteps_playwright_browser_interface:
 | 
			
		||||
                print("Starting connection with playwright")
 | 
			
		||||
                logging.debug("browser_steps.py connecting")
 | 
			
		||||
        # Screenshots and other info only needed on requesting a step (POST)
 | 
			
		||||
        try:
 | 
			
		||||
            state = browsersteps_sessions[browsersteps_session_id]['browserstepper'].get_current_state()
 | 
			
		||||
        except playwright._impl._api_types.Error as e:
 | 
			
		||||
            return make_response("Browser session ran out of time :( Please reload this page."+str(e), 401)
 | 
			
		||||
 | 
			
		||||
                global browsersteps_playwright_browser_interface_context
 | 
			
		||||
                from . import nonContext
 | 
			
		||||
                browsersteps_playwright_browser_interface_context = nonContext.c_sync_playwright()
 | 
			
		||||
                browsersteps_playwright_browser_interface = browsersteps_playwright_browser_interface_context.start()
 | 
			
		||||
                # At 20 minutes, some other variable is closing it
 | 
			
		||||
                # @todo find out what it is and set it
 | 
			
		||||
                seconds_keepalive = int(os.getenv('BROWSERSTEPS_MINUTES_KEEPALIVE', 10)) * 60
 | 
			
		||||
        # Use send_file() which is way faster than read/write loop on bytes
 | 
			
		||||
        import json
 | 
			
		||||
        from tempfile import mkstemp
 | 
			
		||||
        from flask import send_file
 | 
			
		||||
        tmp_fd, tmp_file = mkstemp(text=True, suffix=".json", prefix="changedetectionio-")
 | 
			
		||||
 | 
			
		||||
                # keep it alive for 10 seconds more than we advertise, sometimes it helps to keep it shutting down cleanly
 | 
			
		||||
                keepalive = "&timeout={}".format(((seconds_keepalive+3) * 1000))
 | 
			
		||||
                try:
 | 
			
		||||
                    browsersteps_playwright_browser_interface_browser = browsersteps_playwright_browser_interface.chromium.connect_over_cdp(
 | 
			
		||||
                        os.getenv('PLAYWRIGHT_DRIVER_URL', '') + keepalive)
 | 
			
		||||
                except Exception as e:
 | 
			
		||||
                    if 'ECONNREFUSED' in str(e):
 | 
			
		||||
                        return make_response('Unable to start the Playwright session properly, is it running?', 401)
 | 
			
		||||
        output = json.dumps({'screenshot': "data:image/jpeg;base64,{}".format(
 | 
			
		||||
            base64.b64encode(state[0]).decode('ascii')),
 | 
			
		||||
            'xpath_data': state[1],
 | 
			
		||||
            'session_age_start': browsersteps_sessions[browsersteps_session_id]['browserstepper'].age_start,
 | 
			
		||||
            'browser_time_remaining': round(remaining)
 | 
			
		||||
        })
 | 
			
		||||
 | 
			
		||||
                browsersteps_playwright_browser_interface_end_time = time.time() + (seconds_keepalive-3)
 | 
			
		||||
                print("Starting connection with playwright - done")
 | 
			
		||||
        with os.fdopen(tmp_fd, 'w') as f:
 | 
			
		||||
            f.write(output)
 | 
			
		||||
 | 
			
		||||
            if not browsersteps_live_ui_o.get(browsersteps_session_id):
 | 
			
		||||
                # Boot up a new session
 | 
			
		||||
                proxy_id = datastore.get_preferred_proxy_for_watch(uuid=uuid)
 | 
			
		||||
                proxy = None
 | 
			
		||||
                if proxy_id:
 | 
			
		||||
                    proxy_url = datastore.proxy_list.get(proxy_id).get('url')
 | 
			
		||||
                    if proxy_url:
 | 
			
		||||
 | 
			
		||||
                        # Playwright needs separate username and password values
 | 
			
		||||
                        from urllib.parse import urlparse
 | 
			
		||||
                        parsed = urlparse(proxy_url)
 | 
			
		||||
                        proxy = {'server': proxy_url}
 | 
			
		||||
 | 
			
		||||
                        if parsed.username:
 | 
			
		||||
                            proxy['username'] = parsed.username
 | 
			
		||||
 | 
			
		||||
                        if parsed.password:
 | 
			
		||||
                            proxy['password'] = parsed.password
 | 
			
		||||
 | 
			
		||||
                        print("Browser Steps: UUID {} Using proxy {}".format(uuid, proxy_url))
 | 
			
		||||
 | 
			
		||||
                # Begin the new "Playwright Context" that re-uses the playwright interface
 | 
			
		||||
                # Each session is a "Playwright Context" as a list, that uses the playwright interface
 | 
			
		||||
                browsersteps_live_ui_o[browsersteps_session_id] = browser_steps.browsersteps_live_ui(
 | 
			
		||||
                    playwright_browser=browsersteps_playwright_browser_interface_browser,
 | 
			
		||||
                    proxy=proxy)
 | 
			
		||||
                this_session = browsersteps_live_ui_o[browsersteps_session_id]
 | 
			
		||||
 | 
			
		||||
        if not this_session.page:
 | 
			
		||||
            cleanup_playwright_session()
 | 
			
		||||
            return make_response('Browser session ran out of time :( Please reload this page.', 401)
 | 
			
		||||
 | 
			
		||||
        response = None
 | 
			
		||||
 | 
			
		||||
        if request.method == 'POST':
 | 
			
		||||
            # Screenshots and other info only needed on requesting a step (POST)
 | 
			
		||||
            try:
 | 
			
		||||
                state = this_session.get_current_state()
 | 
			
		||||
            except playwright._impl._api_types.Error as e:
 | 
			
		||||
                return make_response("Browser session ran out of time :( Please reload this page."+str(e), 401)
 | 
			
		||||
 | 
			
		||||
            # Use send_file() which is way faster than read/write loop on bytes
 | 
			
		||||
            import json
 | 
			
		||||
            from tempfile import mkstemp
 | 
			
		||||
            from flask import send_file
 | 
			
		||||
            tmp_fd, tmp_file = mkstemp(text=True, suffix=".json", prefix="changedetectionio-")
 | 
			
		||||
 | 
			
		||||
            output = json.dumps({'screenshot': "data:image/jpeg;base64,{}".format(
 | 
			
		||||
                base64.b64encode(state[0]).decode('ascii')),
 | 
			
		||||
                'xpath_data': state[1],
 | 
			
		||||
                'session_age_start': this_session.age_start,
 | 
			
		||||
                'browser_time_remaining': round(remaining)
 | 
			
		||||
            })
 | 
			
		||||
 | 
			
		||||
            with os.fdopen(tmp_fd, 'w') as f:
 | 
			
		||||
                f.write(output)
 | 
			
		||||
 | 
			
		||||
            response = make_response(send_file(path_or_file=tmp_file,
 | 
			
		||||
                                               mimetype='application/json; charset=UTF-8',
 | 
			
		||||
                                               etag=True))
 | 
			
		||||
            # No longer needed
 | 
			
		||||
            os.unlink(tmp_file)
 | 
			
		||||
 | 
			
		||||
        elif request.method == 'GET':
 | 
			
		||||
            # Just enough to get the session rolling, it will call for goto-site via POST next
 | 
			
		||||
            response = make_response({
 | 
			
		||||
                'session_age_start': this_session.age_start,
 | 
			
		||||
                'browser_time_remaining': round(remaining)
 | 
			
		||||
            })
 | 
			
		||||
        response = make_response(send_file(path_or_file=tmp_file,
 | 
			
		||||
                                           mimetype='application/json; charset=UTF-8',
 | 
			
		||||
                                           etag=True))
 | 
			
		||||
        # No longer needed
 | 
			
		||||
        os.unlink(tmp_file)
 | 
			
		||||
 | 
			
		||||
        return response
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -71,10 +71,10 @@ class steppable_browser_interface():
 | 
			
		||||
            optional_value = str(jinja2_env.from_string(optional_value).render())
 | 
			
		||||
 | 
			
		||||
        action_handler(selector, optional_value)
 | 
			
		||||
        self.page.wait_for_timeout(3 * 1000)
 | 
			
		||||
        self.page.wait_for_timeout(1.5 * 1000)
 | 
			
		||||
        print("Call action done in", time.time() - now)
 | 
			
		||||
 | 
			
		||||
    def action_goto_url(self, selector, value):
 | 
			
		||||
    def action_goto_url(self, selector=None, value=None):
 | 
			
		||||
        # self.page.set_viewport_size({"width": 1280, "height": 5000})
 | 
			
		||||
        now = time.time()
 | 
			
		||||
        response = self.page.goto(value, timeout=0, wait_until='commit')
 | 
			
		||||
@@ -105,7 +105,8 @@ class steppable_browser_interface():
 | 
			
		||||
        print("Clicking element")
 | 
			
		||||
        if not len(selector.strip()):
 | 
			
		||||
            return
 | 
			
		||||
        self.page.click(selector, timeout=10 * 1000, delay=randint(200, 500))
 | 
			
		||||
 | 
			
		||||
        self.page.click(selector=selector, timeout=30 * 1000, delay=randint(200, 500))
 | 
			
		||||
 | 
			
		||||
    def action_click_element_if_exists(self, selector, value):
 | 
			
		||||
        import playwright._impl._api_types as _api_types
 | 
			
		||||
@@ -132,18 +133,18 @@ class steppable_browser_interface():
 | 
			
		||||
        self.page.wait_for_timeout(1000)
 | 
			
		||||
 | 
			
		||||
    def action_wait_for_seconds(self, selector, value):
 | 
			
		||||
        self.page.wait_for_timeout(int(value) * 1000)
 | 
			
		||||
        self.page.wait_for_timeout(float(value.strip()) * 1000)
 | 
			
		||||
 | 
			
		||||
    def action_wait_for_text(self, selector, value):
 | 
			
		||||
        import json
 | 
			
		||||
        v = json.dumps(value)
 | 
			
		||||
        self.page.wait_for_function(f'document.querySelector("body").innerText.includes({v});', timeout=30000)
 | 
			
		||||
        self.page.wait_for_function(f'document.querySelector("body").innerText.includes({v});', timeout=90000)
 | 
			
		||||
 | 
			
		||||
    def action_wait_for_text_in_element(self, selector, value):
 | 
			
		||||
        import json
 | 
			
		||||
        s = json.dumps(selector)
 | 
			
		||||
        v = json.dumps(value)
 | 
			
		||||
        self.page.wait_for_function(f'document.querySelector({s}).innerText.includes({v});', timeout=30000)
 | 
			
		||||
        self.page.wait_for_function(f'document.querySelector({s}).innerText.includes({v});', timeout=90000)
 | 
			
		||||
 | 
			
		||||
    # @todo - in the future make some popout interface to capture what needs to be set
 | 
			
		||||
    # https://playwright.dev/python/docs/api/class-keyboard
 | 
			
		||||
 
 | 
			
		||||
@@ -182,7 +182,8 @@ class Fetcher():
 | 
			
		||||
                                                      optional_value=optional_value)
 | 
			
		||||
                    self.screenshot_step(step_n)
 | 
			
		||||
                    self.save_step_html(step_n)
 | 
			
		||||
                except TimeoutError:
 | 
			
		||||
                except TimeoutError as e:
 | 
			
		||||
                    print(str(e))
 | 
			
		||||
                    # Stop processing here
 | 
			
		||||
                    raise BrowserStepsStepTimout(step_n=step_n)
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -49,3 +49,15 @@ class model(dict):
 | 
			
		||||
    def __init__(self, *arg, **kw):
 | 
			
		||||
        super(model, self).__init__(*arg, **kw)
 | 
			
		||||
        self.update(self.base_config)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def parse_headers_from_text_file(filepath):
 | 
			
		||||
    headers = {}
 | 
			
		||||
    with open(filepath, 'r') as f:
 | 
			
		||||
        for l in f.readlines():
 | 
			
		||||
            l = l.strip()
 | 
			
		||||
            if not l.startswith('#') and ':' in l:
 | 
			
		||||
                (k, v) = l.split(':')
 | 
			
		||||
                headers[k.strip()] = v.strip()
 | 
			
		||||
 | 
			
		||||
    return headers
 | 
			
		||||
@@ -473,6 +473,40 @@ class model(dict):
 | 
			
		||||
        # None is set
 | 
			
		||||
        return False
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def has_extra_headers_file(self):
 | 
			
		||||
        if os.path.isfile(os.path.join(self.watch_data_dir, 'headers.txt')):
 | 
			
		||||
            return True
 | 
			
		||||
 | 
			
		||||
        for f in self.all_tags:
 | 
			
		||||
            fname = "headers-"+re.sub(r'[\W_]', '', f).lower().strip() + ".txt"
 | 
			
		||||
            filepath = os.path.join(self.__datastore_path, fname)
 | 
			
		||||
            if os.path.isfile(filepath):
 | 
			
		||||
                return True
 | 
			
		||||
 | 
			
		||||
        return False
 | 
			
		||||
 | 
			
		||||
    def get_all_headers(self):
 | 
			
		||||
        from .App import parse_headers_from_text_file
 | 
			
		||||
        headers = self.get('headers', {}).copy()
 | 
			
		||||
        # Available headers on the disk could 'headers.txt' in the watch data dir
 | 
			
		||||
        filepath = os.path.join(self.watch_data_dir, 'headers.txt')
 | 
			
		||||
        try:
 | 
			
		||||
            if os.path.isfile(filepath):
 | 
			
		||||
                headers.update(parse_headers_from_text_file(filepath))
 | 
			
		||||
        except Exception as e:
 | 
			
		||||
            print(f"ERROR reading headers.txt at {filepath}", str(e))
 | 
			
		||||
 | 
			
		||||
        # Or each by tag, as tagname.txt in the main datadir
 | 
			
		||||
        for f in self.all_tags:
 | 
			
		||||
            fname = "headers-"+re.sub(r'[\W_]', '', f).lower().strip() + ".txt"
 | 
			
		||||
            filepath = os.path.join(self.__datastore_path, fname)
 | 
			
		||||
            try:
 | 
			
		||||
                if os.path.isfile(filepath):
 | 
			
		||||
                    headers.update(parse_headers_from_text_file(filepath))
 | 
			
		||||
            except Exception as e:
 | 
			
		||||
                print(f"ERROR reading headers.txt at {filepath}", str(e))
 | 
			
		||||
        return headers
 | 
			
		||||
 | 
			
		||||
    def get_last_fetched_before_filters(self):
 | 
			
		||||
        import brotli
 | 
			
		||||
 
 | 
			
		||||
@@ -70,10 +70,9 @@ class perform_site_check(difference_detection_processor):
 | 
			
		||||
        # Unset any existing notification error
 | 
			
		||||
        update_obj = {'last_notification_error': False, 'last_error': False}
 | 
			
		||||
 | 
			
		||||
        extra_headers = watch.get('headers', [])
 | 
			
		||||
 | 
			
		||||
        # Tweak the base config with the per-watch ones
 | 
			
		||||
        request_headers = deepcopy(self.datastore.data['settings']['headers'])
 | 
			
		||||
        extra_headers = watch.get_all_headers()
 | 
			
		||||
        request_headers = self.datastore.get_all_headers()
 | 
			
		||||
        request_headers.update(extra_headers)
 | 
			
		||||
 | 
			
		||||
        # https://github.com/psf/requests/issues/4525
 | 
			
		||||
 
 | 
			
		||||
@@ -162,7 +162,9 @@ $(document).ready(function () {
 | 
			
		||||
                ) {
 | 
			
		||||
                    // There could be many elements here, record them all and then we'll find out which is the most 'useful'
 | 
			
		||||
                    // (input, textarea, button, A etc)
 | 
			
		||||
                    possible_elements.push(item);
 | 
			
		||||
                    if (item.width < xpath_data['browser_width']) {
 | 
			
		||||
                        possible_elements.push(item);
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            });
 | 
			
		||||
 | 
			
		||||
@@ -238,7 +240,7 @@ $(document).ready(function () {
 | 
			
		||||
 | 
			
		||||
    function start() {
 | 
			
		||||
        console.log("Starting browser-steps UI");
 | 
			
		||||
        browsersteps_session_id = Date.now();
 | 
			
		||||
        browsersteps_session_id = false;
 | 
			
		||||
        // @todo This setting of the first one should be done at the datalayer but wtforms doesnt wanna play nice
 | 
			
		||||
        $('#browser_steps >li:first-child').removeClass('empty');
 | 
			
		||||
        set_first_gotosite_disabled();
 | 
			
		||||
@@ -246,7 +248,7 @@ $(document).ready(function () {
 | 
			
		||||
        $('.clear,.remove', $('#browser_steps >li:first-child')).hide();
 | 
			
		||||
        $.ajax({
 | 
			
		||||
            type: "GET",
 | 
			
		||||
            url: browser_steps_sync_url + "&browsersteps_session_id=" + browsersteps_session_id,
 | 
			
		||||
            url: browser_steps_start_url,
 | 
			
		||||
            statusCode: {
 | 
			
		||||
                400: function () {
 | 
			
		||||
                    // More than likely the CSRF token was lost when the server restarted
 | 
			
		||||
@@ -254,12 +256,12 @@ $(document).ready(function () {
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }).done(function (data) {
 | 
			
		||||
            xpath_data = data.xpath_data;
 | 
			
		||||
            $("#loading-status-text").fadeIn();
 | 
			
		||||
            browsersteps_session_id = data.browsersteps_session_id;
 | 
			
		||||
            // This should trigger 'Goto site'
 | 
			
		||||
            console.log("Got startup response, requesting Goto-Site (first) step fake click");
 | 
			
		||||
            $('#browser_steps >li:first-child .apply').click();
 | 
			
		||||
            browserless_seconds_remaining = data.browser_time_remaining;
 | 
			
		||||
            browserless_seconds_remaining = 500;
 | 
			
		||||
            set_first_gotosite_disabled();
 | 
			
		||||
        }).fail(function (data) {
 | 
			
		||||
            console.log(data);
 | 
			
		||||
@@ -420,7 +422,6 @@ $(document).ready(function () {
 | 
			
		||||
            apply_buttons_disabled = false;
 | 
			
		||||
            $("#browsersteps-img").css('opacity', 1);
 | 
			
		||||
            $('ul#browser_steps li .control .apply').css('opacity', 1);
 | 
			
		||||
            browserless_seconds_remaining = data.browser_time_remaining;
 | 
			
		||||
            $("#loading-status-text").hide();
 | 
			
		||||
            set_first_gotosite_disabled();
 | 
			
		||||
        }).fail(function (data) {
 | 
			
		||||
 
 | 
			
		||||
@@ -3,7 +3,7 @@
 | 
			
		||||
 * Toggles theme between light and dark mode.
 | 
			
		||||
 */
 | 
			
		||||
$(document).ready(function () {
 | 
			
		||||
  const button = document.getElementsByClassName("toggle-theme")[0];
 | 
			
		||||
  const button = document.getElementById("toggle-light-mode");
 | 
			
		||||
 | 
			
		||||
  button.onclick = () => {
 | 
			
		||||
    const htmlElement = document.getElementsByTagName("html");
 | 
			
		||||
@@ -21,4 +21,33 @@ $(document).ready(function () {
 | 
			
		||||
  const setCookieValue = (value) => {
 | 
			
		||||
    document.cookie = `css_dark_mode=${value};max-age=31536000;path=/`
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Search input box behaviour
 | 
			
		||||
    const toggle_search = document.getElementById("toggle-search");
 | 
			
		||||
  const search_q = document.getElementById("search-q");
 | 
			
		||||
  window.addEventListener('keydown', function (e) {
 | 
			
		||||
 | 
			
		||||
    if (e.altKey == true && e.keyCode == 83)
 | 
			
		||||
      search_q.classList.toggle('expanded');
 | 
			
		||||
      search_q.focus();
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
  search_q.onkeydown = (e) => {
 | 
			
		||||
    var key = e.keyCode || e.which;
 | 
			
		||||
    if (key === 13) {
 | 
			
		||||
      document.searchForm.submit();
 | 
			
		||||
    }
 | 
			
		||||
  };
 | 
			
		||||
  toggle_search.onclick = () => {
 | 
			
		||||
    // Could be that they want to search something once text is in there
 | 
			
		||||
    if (search_q.value.length) {
 | 
			
		||||
      document.searchForm.submit();
 | 
			
		||||
    } else {
 | 
			
		||||
      // If not..
 | 
			
		||||
      search_q.classList.toggle('expanded');
 | 
			
		||||
      search_q.focus();
 | 
			
		||||
    }
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
});
 | 
			
		||||
 
 | 
			
		||||
@@ -61,7 +61,12 @@ $(document).ready(function () {
 | 
			
		||||
    function bootstrap_visualselector() {
 | 
			
		||||
        if (1) {
 | 
			
		||||
            // bootstrap it, this will trigger everything else
 | 
			
		||||
            $("img#selector-background").bind('load', function () {
 | 
			
		||||
            $("img#selector-background").on("error", function () {
 | 
			
		||||
                $('.fetching-update-notice').html("<strong>Ooops!</strong> The VisualSelector tool needs atleast one fetched page, please unpause the watch and/or wait for the watch to complete fetching and then reload this page.");
 | 
			
		||||
                $('.fetching-update-notice').css('color','#bb0000');
 | 
			
		||||
                $('#selector-current-xpath').hide();
 | 
			
		||||
                $('#clear-selector').hide();
 | 
			
		||||
            }).bind('load', function () {
 | 
			
		||||
                console.log("Loaded background...");
 | 
			
		||||
                c = document.getElementById("selector-canvas");
 | 
			
		||||
                // greyed out fill context
 | 
			
		||||
@@ -79,10 +84,11 @@ $(document).ready(function () {
 | 
			
		||||
            }).attr("src", screenshot_url);
 | 
			
		||||
        }
 | 
			
		||||
        // Tell visualSelector that the image should update
 | 
			
		||||
        var s = $("img#selector-background").attr('src')+"?"+ new Date().getTime();
 | 
			
		||||
        $("img#selector-background").attr('src',s)
 | 
			
		||||
        var s = $("img#selector-background").attr('src') + "?" + new Date().getTime();
 | 
			
		||||
        $("img#selector-background").attr('src', s)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // This is fired once the img src is loaded in bootstrap_visualselector()
 | 
			
		||||
    function fetch_data() {
 | 
			
		||||
        // Image is ready
 | 
			
		||||
        $('.fetching-update-notice').html("Fetching element data..");
 | 
			
		||||
@@ -99,7 +105,8 @@ $(document).ready(function () {
 | 
			
		||||
            reflow_selector();
 | 
			
		||||
            $('.fetching-update-notice').fadeOut();
 | 
			
		||||
        });
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    function set_scale() {
 | 
			
		||||
 
 | 
			
		||||
@@ -54,8 +54,47 @@ a.github-link {
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
button.toggle-theme {
 | 
			
		||||
  width: 4rem;
 | 
			
		||||
#toggle-light-mode {
 | 
			
		||||
  width: 3rem;
 | 
			
		||||
  .icon-dark {
 | 
			
		||||
    display: none;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  &.dark {
 | 
			
		||||
    .icon-light {
 | 
			
		||||
      display: none;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    .icon-dark {
 | 
			
		||||
      display: block;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#toggle-search {
 | 
			
		||||
  width: 2rem;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#search-q {
 | 
			
		||||
  opacity: 0;
 | 
			
		||||
  -webkit-transition: all .9s ease;
 | 
			
		||||
  -moz-transition: all .9s ease;
 | 
			
		||||
  transition: all .9s ease;
 | 
			
		||||
  width: 0;
 | 
			
		||||
  display: none;
 | 
			
		||||
  &.expanded {
 | 
			
		||||
    width: auto;
 | 
			
		||||
    display: inline-block;
 | 
			
		||||
 | 
			
		||||
    opacity: 1;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
#search-result-info {
 | 
			
		||||
  color: #fff;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
button.toggle-button {
 | 
			
		||||
  vertical-align: middle;
 | 
			
		||||
  background: transparent;
 | 
			
		||||
  border: none;
 | 
			
		||||
  cursor: pointer;
 | 
			
		||||
@@ -74,19 +113,7 @@ button.toggle-theme {
 | 
			
		||||
    display: block;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  .icon-dark {
 | 
			
		||||
    display: none;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  &.dark {
 | 
			
		||||
    .icon-light {
 | 
			
		||||
      display: none;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    .icon-dark {
 | 
			
		||||
      display: block;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
.pure-menu-horizontal {
 | 
			
		||||
 
 | 
			
		||||
@@ -331,23 +331,44 @@ a.github-link {
 | 
			
		||||
  a.github-link:hover {
 | 
			
		||||
    color: var(--color-icon-github-hover); }
 | 
			
		||||
 | 
			
		||||
button.toggle-theme {
 | 
			
		||||
  width: 4rem;
 | 
			
		||||
#toggle-light-mode {
 | 
			
		||||
  width: 3rem; }
 | 
			
		||||
  #toggle-light-mode .icon-dark {
 | 
			
		||||
    display: none; }
 | 
			
		||||
  #toggle-light-mode.dark .icon-light {
 | 
			
		||||
    display: none; }
 | 
			
		||||
  #toggle-light-mode.dark .icon-dark {
 | 
			
		||||
    display: block; }
 | 
			
		||||
 | 
			
		||||
#toggle-search {
 | 
			
		||||
  width: 2rem; }
 | 
			
		||||
 | 
			
		||||
#search-q {
 | 
			
		||||
  opacity: 0;
 | 
			
		||||
  -webkit-transition: all .9s ease;
 | 
			
		||||
  -moz-transition: all .9s ease;
 | 
			
		||||
  transition: all .9s ease;
 | 
			
		||||
  width: 0;
 | 
			
		||||
  display: none; }
 | 
			
		||||
  #search-q.expanded {
 | 
			
		||||
    width: auto;
 | 
			
		||||
    display: inline-block;
 | 
			
		||||
    opacity: 1; }
 | 
			
		||||
 | 
			
		||||
#search-result-info {
 | 
			
		||||
  color: #fff; }
 | 
			
		||||
 | 
			
		||||
button.toggle-button {
 | 
			
		||||
  vertical-align: middle;
 | 
			
		||||
  background: transparent;
 | 
			
		||||
  border: none;
 | 
			
		||||
  cursor: pointer;
 | 
			
		||||
  color: var(--color-icon-github); }
 | 
			
		||||
  button.toggle-theme:hover {
 | 
			
		||||
  button.toggle-button:hover {
 | 
			
		||||
    color: var(--color-icon-github-hover); }
 | 
			
		||||
  button.toggle-theme svg {
 | 
			
		||||
  button.toggle-button svg {
 | 
			
		||||
    fill: currentColor; }
 | 
			
		||||
  button.toggle-theme .icon-light {
 | 
			
		||||
    display: block; }
 | 
			
		||||
  button.toggle-theme .icon-dark {
 | 
			
		||||
    display: none; }
 | 
			
		||||
  button.toggle-theme.dark .icon-light {
 | 
			
		||||
    display: none; }
 | 
			
		||||
  button.toggle-theme.dark .icon-dark {
 | 
			
		||||
  button.toggle-button .icon-light {
 | 
			
		||||
    display: block; }
 | 
			
		||||
 | 
			
		||||
.pure-menu-horizontal {
 | 
			
		||||
 
 | 
			
		||||
@@ -3,7 +3,7 @@ from flask import (
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
from . model import App, Watch
 | 
			
		||||
from copy import deepcopy
 | 
			
		||||
from copy import deepcopy, copy
 | 
			
		||||
from os import path, unlink
 | 
			
		||||
from threading import Lock
 | 
			
		||||
import json
 | 
			
		||||
@@ -366,19 +366,21 @@ class ChangeDetectionStore:
 | 
			
		||||
    def save_error_text(self, watch_uuid, contents):
 | 
			
		||||
        if not self.data['watching'].get(watch_uuid):
 | 
			
		||||
            return
 | 
			
		||||
        target_path = os.path.join(self.datastore_path, watch_uuid, "last-error.txt")
 | 
			
		||||
 | 
			
		||||
        self.data['watching'][watch_uuid].ensure_data_dir_exists()
 | 
			
		||||
        target_path = os.path.join(self.datastore_path, watch_uuid, "last-error.txt")
 | 
			
		||||
        with open(target_path, 'w') as f:
 | 
			
		||||
            f.write(contents)
 | 
			
		||||
 | 
			
		||||
    def save_xpath_data(self, watch_uuid, data, as_error=False):
 | 
			
		||||
 | 
			
		||||
        if not self.data['watching'].get(watch_uuid):
 | 
			
		||||
            return
 | 
			
		||||
        if as_error:
 | 
			
		||||
            target_path = os.path.join(self.datastore_path, watch_uuid, "elements-error.json")
 | 
			
		||||
        else:
 | 
			
		||||
            target_path = os.path.join(self.datastore_path, watch_uuid, "elements.json")
 | 
			
		||||
 | 
			
		||||
        self.data['watching'][watch_uuid].ensure_data_dir_exists()
 | 
			
		||||
        with open(target_path, 'w') as f:
 | 
			
		||||
            f.write(json.dumps(data))
 | 
			
		||||
            f.close()
 | 
			
		||||
@@ -472,8 +474,6 @@ class ChangeDetectionStore:
 | 
			
		||||
        return proxy_list if len(proxy_list) else None
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    def get_preferred_proxy_for_watch(self, uuid):
 | 
			
		||||
        """
 | 
			
		||||
        Returns the preferred proxy by ID key
 | 
			
		||||
@@ -505,6 +505,25 @@ class ChangeDetectionStore:
 | 
			
		||||
 | 
			
		||||
        return None
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def has_extra_headers_file(self):
 | 
			
		||||
        filepath = os.path.join(self.datastore_path, 'headers.txt')
 | 
			
		||||
        return os.path.isfile(filepath)
 | 
			
		||||
 | 
			
		||||
    def get_all_headers(self):
 | 
			
		||||
        from .model.App import parse_headers_from_text_file
 | 
			
		||||
        headers = copy(self.data['settings'].get('headers', {}))
 | 
			
		||||
 | 
			
		||||
        filepath = os.path.join(self.datastore_path, 'headers.txt')
 | 
			
		||||
        try:
 | 
			
		||||
            if os.path.isfile(filepath):
 | 
			
		||||
                headers.update(parse_headers_from_text_file(filepath))
 | 
			
		||||
        except Exception as e:
 | 
			
		||||
            print(f"ERROR reading headers.txt at {filepath}", str(e))
 | 
			
		||||
 | 
			
		||||
        return headers
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    # Run all updates
 | 
			
		||||
    # IMPORTANT - Each update could be run even when they have a new install and the schema is correct
 | 
			
		||||
    #             So therefor - each `update_n` should be very careful about checking if it needs to actually run
 | 
			
		||||
 
 | 
			
		||||
@@ -115,7 +115,7 @@
 | 
			
		||||
                                    URLs generated by changedetection.io (such as <code>{{ '{{diff_url}}' }}</code>) require the <code>BASE_URL</code> environment variable set.<br>
 | 
			
		||||
                                    Your <code>BASE_URL</code> var is currently "{{settings_application['current_base_url']}}"
 | 
			
		||||
									<br>
 | 
			
		||||
									Warning: Contents of <code>{{ '{{diff}}' }}</code>, <code>{{ '{{diff_removed}}' }}</code>, and <code>{{ '{{diff_added}}' }}</code> depend on how the difference algorithm perceives the change. For example, an addition or removal could be perceived as a change in some cases. <a target="_new" href="https://github.com/dgtlmoon/changedetection.io/wiki/Using-the-%7B%7Bdiff%7D%7D,-%7B%7Bdiff_added%7D%7D,-and-%7B%7Bdiff_removal%7D%7D-notification-tokens">More Here</a> <br>
 | 
			
		||||
									Warning: Contents of <code>{{ '{{diff}}' }}</code>, <code>{{ '{{diff_removed}}' }}</code>, and <code>{{ '{{diff_added}}' }}</code> depend on how the difference algorithm perceives the change. For example, an addition or removal could be perceived as a change in some cases. <a target="_new" href="https://github.com/dgtlmoon/changedetection.io/wiki/Using-the-%7B%7Bdiff%7D%7D,-%7B%7Bdiff_added%7D%7D,-and-%7B%7Bdiff_removed%7D%7D-notification-tokens">More Here</a> <br>
 | 
			
		||||
                                </div>
 | 
			
		||||
                            </div>
 | 
			
		||||
                        </div>
 | 
			
		||||
 
 | 
			
		||||
@@ -82,11 +82,21 @@
 | 
			
		||||
              <a href="{{url_for('logout')}}" class="pure-menu-link">LOG OUT</a>
 | 
			
		||||
            </li>
 | 
			
		||||
          {% endif %}
 | 
			
		||||
          <li class="pure-menu-item pure-form" id="search-menu-item">
 | 
			
		||||
            <!-- We use GET here so it offers people a chance to set bookmarks etc -->
 | 
			
		||||
            <form name="searchForm" action="" method="GET">
 | 
			
		||||
              <input id="search-q" class="" name="q" placeholder="URL or Title {% if active_tag %}in '{{ active_tag }}'{% endif %}" required="" type="text" value="">
 | 
			
		||||
              <input name="tag" type="hidden" value="{% if active_tag %}{{active_tag}}{% endif %}">
 | 
			
		||||
              <button class="toggle-button " id="toggle-search" type="button" title="Search, or Use Alt+S Key" >
 | 
			
		||||
                {% include "svgs/search-icon.svg" %}
 | 
			
		||||
              </button>
 | 
			
		||||
            </form>
 | 
			
		||||
          </li>
 | 
			
		||||
          <li class="pure-menu-item">
 | 
			
		||||
            {% if dark_mode %}
 | 
			
		||||
            {% set darkClass = 'dark' %}
 | 
			
		||||
            {% endif %}
 | 
			
		||||
            <button class="toggle-theme {{darkClass}}" type="button" title="Toggle Light/Dark Mode">
 | 
			
		||||
            <button class="toggle-button {{darkClass}}"  id ="toggle-light-mode" type="button" title="Toggle Light/Dark Mode">
 | 
			
		||||
              <span class="visually-hidden">Toggle light/dark mode</span>
 | 
			
		||||
              <span class="icon-light">
 | 
			
		||||
                {% include "svgs/light-mode-toggle-icon.svg" %}
 | 
			
		||||
@@ -106,7 +116,7 @@
 | 
			
		||||
    </div>
 | 
			
		||||
    {% if hosted_sticky %}
 | 
			
		||||
      <div class="sticky-tab" id="hosted-sticky">
 | 
			
		||||
        <a href="https://lemonade.changedetection.io/start?ref={{guid}}">Let us host your instance!</a>
 | 
			
		||||
        <a href="https://changedetection.io/?ref={{guid}}">Let us host your instance!</a>
 | 
			
		||||
      </div>
 | 
			
		||||
    {% endif %}
 | 
			
		||||
    {% if left_sticky %}
 | 
			
		||||
 
 | 
			
		||||
@@ -14,7 +14,9 @@
 | 
			
		||||
{% endif %}
 | 
			
		||||
 | 
			
		||||
    const browser_steps_config=JSON.parse('{{ browser_steps_config|tojson }}');
 | 
			
		||||
    const browser_steps_start_url="{{url_for('browser_steps.browsersteps_start_session', uuid=uuid)}}";
 | 
			
		||||
    const browser_steps_sync_url="{{url_for('browser_steps.browsersteps_ui_update', uuid=uuid)}}";
 | 
			
		||||
 | 
			
		||||
</script>
 | 
			
		||||
 | 
			
		||||
<script src="{{url_for('static_content', group='js', filename='watch-settings.js')}}" defer></script>
 | 
			
		||||
@@ -150,6 +152,15 @@
 | 
			
		||||
{{ render_field(form.headers, rows=5, placeholder="Example
 | 
			
		||||
Cookie: foobar
 | 
			
		||||
User-Agent: wonderbra 1.0") }}
 | 
			
		||||
 | 
			
		||||
                        <div class="pure-form-message-inline">
 | 
			
		||||
                            {% if has_extra_headers_file %}
 | 
			
		||||
                                <strong>Alert! Extra headers file found and will be added to this watch!</strong>
 | 
			
		||||
                            {% else %}
 | 
			
		||||
                                Headers can be also read from a file in your data-directory <a href="https://github.com/dgtlmoon/changedetection.io/wiki/Adding-headers-from-an-external-file">Read more here</a>
 | 
			
		||||
                            {% endif %}
 | 
			
		||||
                        </div>
 | 
			
		||||
 | 
			
		||||
                    </div>
 | 
			
		||||
                    <div class="pure-control-group" id="request-body">
 | 
			
		||||
                                        {{ render_field(form.body, rows=5, placeholder="Example
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										1
									
								
								changedetectionio/templates/svgs/search-icon.svg
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								changedetectionio/templates/svgs/search-icon.svg
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
<?xml version="1.0" encoding="utf-8"?><svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 122.879 119.799" enable-background="new 0 0 122.879 119.799" xml:space="preserve"><g><path d="M49.988,0h0.016v0.007C63.803,0.011,76.298,5.608,85.34,14.652c9.027,9.031,14.619,21.515,14.628,35.303h0.007v0.033v0.04 h-0.007c-0.005,5.557-0.917,10.905-2.594,15.892c-0.281,0.837-0.575,1.641-0.877,2.409v0.007c-1.446,3.66-3.315,7.12-5.547,10.307 l29.082,26.139l0.018,0.016l0.157,0.146l0.011,0.011c1.642,1.563,2.536,3.656,2.649,5.78c0.11,2.1-0.543,4.248-1.979,5.971 l-0.011,0.016l-0.175,0.203l-0.035,0.035l-0.146,0.16l-0.016,0.021c-1.565,1.642-3.654,2.534-5.78,2.646 c-2.097,0.111-4.247-0.54-5.971-1.978l-0.015-0.011l-0.204-0.175l-0.029-0.024L78.761,90.865c-0.88,0.62-1.778,1.209-2.687,1.765 c-1.233,0.755-2.51,1.466-3.813,2.115c-6.699,3.342-14.269,5.222-22.272,5.222v0.007h-0.016v-0.007 c-13.799-0.004-26.296-5.601-35.338-14.645C5.605,76.291,0.016,63.805,0.007,50.021H0v-0.033v-0.016h0.007 c0.004-13.799,5.601-26.296,14.645-35.338C23.683,5.608,36.167,0.016,49.955,0.007V0H49.988L49.988,0z M50.004,11.21v0.007h-0.016 h-0.033V11.21c-10.686,0.007-20.372,4.35-27.384,11.359C15.56,29.578,11.213,39.274,11.21,49.973h0.007v0.016v0.033H11.21 c0.007,10.686,4.347,20.367,11.359,27.381c7.009,7.012,16.705,11.359,27.403,11.361v-0.007h0.016h0.033v0.007 c10.686-0.007,20.368-4.348,27.382-11.359c7.011-7.009,11.358-16.702,11.36-27.4h-0.006v-0.016v-0.033h0.006 c-0.006-10.686-4.35-20.372-11.358-27.384C70.396,15.56,60.703,11.213,50.004,11.21L50.004,11.21z"/></g></svg>
 | 
			
		||||
| 
		 After Width: | Height: | Size: 1.6 KiB  | 
@@ -44,6 +44,7 @@
 | 
			
		||||
    {% if watches|length >= pagination.per_page %}
 | 
			
		||||
        {{ pagination.info }}
 | 
			
		||||
    {% endif %}
 | 
			
		||||
    {% if search_q %}<div id="search-result-info">Searching "<strong><i>{{search_q}}</i></strong>"</div>{% endif %}
 | 
			
		||||
    <div>
 | 
			
		||||
        <a href="{{url_for('index')}}" class="pure-button button-tag {{'active' if not active_tag }}">All</a>
 | 
			
		||||
        {% for tag in tags %}
 | 
			
		||||
@@ -73,7 +74,11 @@
 | 
			
		||||
            </tr>
 | 
			
		||||
            </thead>
 | 
			
		||||
            <tbody>
 | 
			
		||||
 | 
			
		||||
            {% if not watches|length %}
 | 
			
		||||
            <tr>
 | 
			
		||||
                <td colspan="6">No website watches configured, please add a URL in the box above, or <a href="{{ url_for('import_page')}}" >import a list</a>.</td>
 | 
			
		||||
            </tr>
 | 
			
		||||
            {% endif %}
 | 
			
		||||
            {% for watch in (watches|sort(attribute=sort_attribute, reverse=sort_order == 'asc'))[pagination.skip:pagination.skip+pagination.per_page] %}
 | 
			
		||||
            <tr id="{{ watch.uuid }}"
 | 
			
		||||
                class="{{ loop.cycle('pure-table-odd', 'pure-table-even') }} processor-{{ watch['processor'] }}
 | 
			
		||||
 
 | 
			
		||||
@@ -14,13 +14,16 @@ global app
 | 
			
		||||
 | 
			
		||||
def cleanup(datastore_path):
 | 
			
		||||
    # Unlink test output files
 | 
			
		||||
    files = ['output.txt',
 | 
			
		||||
             'url-watches.json',
 | 
			
		||||
             'secret.txt',
 | 
			
		||||
             'notification.txt',
 | 
			
		||||
             'count.txt',
 | 
			
		||||
             'endpoint-content.txt'
 | 
			
		||||
                 ]
 | 
			
		||||
    files = [
 | 
			
		||||
        'count.txt',
 | 
			
		||||
        'endpoint-content.txt'
 | 
			
		||||
        'headers.txt',
 | 
			
		||||
        'headers-testtag.txt',
 | 
			
		||||
        'notification.txt',
 | 
			
		||||
        'secret.txt',
 | 
			
		||||
        'url-watches.json',
 | 
			
		||||
        'output.txt',
 | 
			
		||||
    ]
 | 
			
		||||
    for file in files:
 | 
			
		||||
        try:
 | 
			
		||||
            os.unlink("{}/{}".format(datastore_path, file))
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,8 @@
 | 
			
		||||
import json
 | 
			
		||||
import os
 | 
			
		||||
import time
 | 
			
		||||
from flask import url_for
 | 
			
		||||
from . util import set_original_response, set_modified_response, live_server_setup
 | 
			
		||||
from . util import set_original_response, set_modified_response, live_server_setup, wait_for_all_checks, extract_UUID_from_client
 | 
			
		||||
 | 
			
		||||
def test_setup(live_server):
 | 
			
		||||
    live_server_setup(live_server)
 | 
			
		||||
@@ -234,3 +235,72 @@ def test_method_in_request(client, live_server):
 | 
			
		||||
    # Should be only one with method set to PATCH
 | 
			
		||||
    assert watches_with_method == 1
 | 
			
		||||
 | 
			
		||||
    res = client.get(url_for("form_delete", uuid="all"), follow_redirects=True)
 | 
			
		||||
    assert b'Deleted' in res.data
 | 
			
		||||
 | 
			
		||||
def test_headers_textfile_in_request(client, live_server):
 | 
			
		||||
    #live_server_setup(live_server)
 | 
			
		||||
    # Add our URL to the import page
 | 
			
		||||
    test_url = url_for('test_headers', _external=True)
 | 
			
		||||
 | 
			
		||||
    # Add the test URL twice, we will check
 | 
			
		||||
    res = client.post(
 | 
			
		||||
        url_for("import_page"),
 | 
			
		||||
        data={"urls": test_url},
 | 
			
		||||
        follow_redirects=True
 | 
			
		||||
    )
 | 
			
		||||
    assert b"1 Imported" in res.data
 | 
			
		||||
 | 
			
		||||
    time.sleep(1)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    # Add some headers to a request
 | 
			
		||||
    res = client.post(
 | 
			
		||||
        url_for("edit_page", uuid="first"),
 | 
			
		||||
        data={
 | 
			
		||||
              "url": test_url,
 | 
			
		||||
              "tag": "testtag",
 | 
			
		||||
              "fetch_backend": "html_requests",
 | 
			
		||||
              "headers": "xxx:ooo\ncool:yeah\r\n"},
 | 
			
		||||
        follow_redirects=True
 | 
			
		||||
    )
 | 
			
		||||
    assert b"Updated watch." in res.data
 | 
			
		||||
    wait_for_all_checks(client)
 | 
			
		||||
 | 
			
		||||
    with open('test-datastore/headers-testtag.txt', 'w') as f:
 | 
			
		||||
        f.write("tag-header: test")
 | 
			
		||||
 | 
			
		||||
    with open('test-datastore/headers.txt', 'w') as f:
 | 
			
		||||
        f.write("global-header: nice\r\nnext-global-header: nice")
 | 
			
		||||
 | 
			
		||||
    with open('test-datastore/'+extract_UUID_from_client(client)+'/headers.txt', 'w') as f:
 | 
			
		||||
        f.write("watch-header: nice")
 | 
			
		||||
 | 
			
		||||
    client.get(url_for("form_watch_checknow"), follow_redirects=True)
 | 
			
		||||
 | 
			
		||||
    # Give the thread time to pick it up
 | 
			
		||||
    wait_for_all_checks(client)
 | 
			
		||||
 | 
			
		||||
    res = client.get(url_for("edit_page", uuid="first"))
 | 
			
		||||
    assert b"Extra headers file found and will be added to this watch" in res.data
 | 
			
		||||
 | 
			
		||||
    # Not needed anymore
 | 
			
		||||
    os.unlink('test-datastore/headers.txt')
 | 
			
		||||
    os.unlink('test-datastore/headers-testtag.txt')
 | 
			
		||||
    os.unlink('test-datastore/'+extract_UUID_from_client(client)+'/headers.txt')
 | 
			
		||||
    # The service should echo back the request verb
 | 
			
		||||
    res = client.get(
 | 
			
		||||
        url_for("preview_page", uuid="first"),
 | 
			
		||||
        follow_redirects=True
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    assert b"Global-Header:nice" in res.data
 | 
			
		||||
    assert b"Next-Global-Header:nice" in res.data
 | 
			
		||||
    assert b"Xxx:ooo" in res.data
 | 
			
		||||
    assert b"Watch-Header:nice" in res.data
 | 
			
		||||
    assert b"Tag-Header:test" in res.data
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    #unlink headers.txt on start/stop
 | 
			
		||||
    res = client.get(url_for("form_delete", uuid="all"), follow_redirects=True)
 | 
			
		||||
    assert b'Deleted' in res.data
 | 
			
		||||
@@ -262,6 +262,7 @@ class update_worker(threading.Thread):
 | 
			
		||||
                        # Yes fine, so nothing todo, don't continue to process.
 | 
			
		||||
                        process_changedetection_results = False
 | 
			
		||||
                        changed_detected = False
 | 
			
		||||
                        self.datastore.update_watch(uuid=uuid, update_obj={'last_error': False})
 | 
			
		||||
 | 
			
		||||
                    except content_fetcher.BrowserStepsStepTimout as e:
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user