Skip to content
This repository has been archived by the owner on Jan 15, 2023. It is now read-only.

Howto: Get page source text #352

Closed
mattkol opened this issue Dec 21, 2021 · 0 comments
Closed

Howto: Get page source text #352

mattkol opened this issue Dec 21, 2021 · 0 comments

Comments

@mattkol
Copy link
Member

mattkol commented Dec 21, 2021

To get page source use Frame GetSource:

public void GetSource(CefStringVisitor visitor)

CrossPlatDemo.zip

  1. Create a handler to get the source:
 internal class SourceTextHandler 
    {
        protected IChromelyConfiguration _config;

        public SourceTextHandler(IChromelyConfiguration config)
        {
            _config = config;
        }

        public void WriteSourceTextToFile(string filename)
        {
            CefFrame frame = _config?.JavaScriptExecutor?.GetMainFrame() as CefFrame;
            if (frame == null)
            {
                return;
            }

            var visitor = new SourceVisitor((text) =>
                                            {
                                                if (!string.IsNullOrWhiteSpace(text))
                                                {
                                                    System.IO.File.WriteAllText(filename, text);
                                                }
                                            });

            frame.GetSource(visitor);
        }

        private sealed class SourceVisitor : CefStringVisitor
        {
            private readonly Action<string> _callback;

            public SourceVisitor(Action<string> callback)
            {
                _callback = callback;
            }

            protected override void Visit(string value)
            {
                _callback(value);
            }
        }
    }
  1. Create a command action:
        [CommandAction(RouteKey = "/democontroller/getsource")]
        public void Getsource(IDictionary<string, string> queryParameters)
        {
            var textHandler = new SourceTextHandler(_config);
            textHandler.WriteSourceTextToFile("source_text.txt");
        }
  1. Add html button:
<a href="http://command.com/democontroller/getsource" class="btn btn-primary" role="button" style='margin: 5px;'>get source</a>

Ref: https://www.magpcss.org/ceforum/viewtopic.php?f=6&t=929

image

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant