7

私はこれが可能かどうか調べようとしています。クラスのソースコードを提供してくれたGitHubの例https://github.com/chillitom/CefSharpを確認しました(ただし、このGITHUBからCefSharp自体をビルドすることはできませんでした。

ただし、このリンクhttps://github.com/downloads/ataranto/CefSharp/CefSharp-1.19.0.7zからバイナリをダウンロードしてみてから、これらの例を参照してC#win32アプリを作成しました。これは、かなりスムーズに進みました。 8時間ほどで、動作する組み込みブラウザyipeeeを入手しました。しかし、私は今、DOMを操作したいところにいます-これはwebView.EvaluateScript( "some script");でのみ実行できることを読みました。およびwebView.ExecuteScript( "some script"); cefsharpを介して直接DOMアクセスを利用できないため

だから私が見つけようとしているのはです。jQueryメソッドを呼び出すことはできますか?ロードしたページにすでにjQueryがロードされている場合、c#で次のことを実行できますか?

webView.ExecuteScript("$(\"input#some_id\").val(\"user@example.com\")"));

現在、これは例外をスローします。私は見つけようとしています。cefsharp DLLからjQueryを使用しようとする必要がありますか、それとも、書き込みに5倍の時間がかかる標準の古い学校のJavaScriptに固執する必要がありますか...?

スタッカーが答えを持ってくれることを願っています。私はcefsharpのウィキとフォーラムを試しましたが、それらはリードの方法で多くを提供していません。私が見つけた唯一の例は、古い学校のJavaScriptです。

4

2 に答える 2

6

はい、jQuery を使用できますが、DOM が完全にロードされた後にのみ使用できます。これを行うには、WebView の PropertyChanged イベントを使用して、IsLoading プロパティが false に変更され、IsBrowserInitialized プロパティが true に設定されていることを確認する必要があります。

私のプロジェクトの 1 つでそれを行う方法のスニペットを以下に示します。ご覧のとおり、IsLoading プロパティが変更されたら、WebView のコンテンツをセットアップするいくつかのメソッドを呼び出します。これは、実行中のように ExecuteScript を介して jQuery を呼び出すことによって行われます。

/// <summary>
/// Initialise the WebView control
/// </summary>
private void InitialiseWebView()
{
    // Disable caching.
    BrowserSettings settings = new BrowserSettings();
    settings.ApplicationCacheDisabled = true;
    settings.PageCacheDisabled = true;

    // Initialise the WebView.
    this.webView = new WebView(string.Empty, settings);
    this.WebView.Name = string.Format("{0}WebBrowser", this.Name);
    this.WebView.Dock = DockStyle.Fill;

    // Setup and regsiter the marshal for the WebView.
    this.chromiumMarshal = new ChromiumMarshal(new Action(() => { this.FlushQueuedMessages(); this.initialising = false; }));
    this.WebView.RegisterJsObject("marshal", this.chromiumMarshal);

    // Setup the event handlers for the WebView.
    this.WebView.PropertyChanged += this.WebView_PropertyChanged;
    this.WebView.PreviewKeyDown += new PreviewKeyDownEventHandler(this.WebView_PreviewKeyDown);

    this.Controls.Add(this.WebView);
}

/// <summary>
/// Handles the PropertyChanged event of CefSharp.WinForms.WebView.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The event arguments.</param>
private void WebView_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
    // Once the browser is initialised, load the HTML for the tab.
    if (!this.webViewIsReady)
    {
        if (e.PropertyName.Equals("IsBrowserInitialized", StringComparison.OrdinalIgnoreCase))
        {
            this.webViewIsReady = this.WebView.IsBrowserInitialized;
            if (this.webViewIsReady)
            {
                string resourceName = "Yaircc.UI.default.htm";
                using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
                {
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        this.WebView.LoadHtml(reader.ReadToEnd());
                    }
                }
            }
        }
    }

    // Once the HTML has finished loading, begin loading the initial content.
    if (e.PropertyName.Equals("IsLoading", StringComparison.OrdinalIgnoreCase))
    {
        if (!this.WebView.IsLoading)
        {
            this.SetSplashText();
            if (this.type == IRCTabType.Console)
            {
                this.SetupConsoleContent();
            }

            GlobalSettings settings = GlobalSettings.Instance;
            this.LoadTheme(settings.ThemeFileName);

            if (this.webViewInitialised != null)
            {
                this.webViewInitialised.Invoke();
            }
        }
    }
}
于 2013-02-19T20:25:09.220 に答える
1

あなたがしたいことは、まだ jQuery を持っていないページに jQuery をロードすることであることをコメントでより明確にしました。ExecuteScriptjQuery ソースのローカル コピーでの実行に関する int0x90 の提案は、うまくいくかもしれません。ただし、注意したいのは、多くのページは、作成者がそこに入れたことのないすべての余分な JS と互換性がないということです。2 つの大きな例は、Google と Facebook です。$どちらもjQuery ではない演算子を定義している$ため、それを踏むとほぼ確実に壊れます。

基礎となる CEF ライブラリは、C++ から DOM 要素を直接操作するための多くのメソッドを公開していますが、CefSharp はまだあまり需要がないため公開していません。ただし、それが実際にここで使用したいもののようです。CefSharp のソースを見れば、それらを公開するのは大した作業ではないかもしれませんが、私自身は試していません。試してみたい場合は、https://groups.google.com/forum/# !forum/cefsharp に質問を投稿することもできます。

于 2013-02-21T19:41:45.630 に答える