[Solved]CEFSharp ExecuteScriptAsync execute css script?

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

[Solved]CEFSharp ExecuteScriptAsync execute css script?

Postby Shirotzu » Sat Sep 12, 2015 2:56 pm

Hey, now that i know a little of ExecuteScriptSync in CEFSharp
i want add this code :
Code: Select all
function loadjscssfile(filename, filetype){
    if (filetype=="css"){
        var fileref=document.createElement("link")
        fileref.setAttribute("rel", "stylesheet")
        fileref.setAttribute("type", "text/css")
        fileref.setAttribute("href", filename)
    }
    if (typeof fileref!="undefined")
        document.getElementsByTagName("head")[0].appendChild(fileref)
}
 
loadjscssfile("mystyle.css", "css")


Like This :
Code: Select all
            if (ChromiumLoadingState == "Ready")
            {
                string script = "function loadjscssfile(filename, filetype){" +
                    @"if (filetype==""css""){" +
                    @"var fileref=document.createElement(""link"")" +
                    @"fileref.setAttribute(""rel"", ""stylesheet"")" +
                    @"fileref.setAttribute(""type"", ""text/css"")" +
                    @"fileref.setAttribute(""href"", filename)" + "}" +
                    @"if (typeof fileref!=""undefined"")" +
                    @"document.getElementsByTagName(""head"")[0].appendChild(fileref)" +
                    @"}" +
                    @"loadjscssfile(""http://trafficmonsoon.byethost32.com/script.css"", ""css"")"; //This code add a css line in Head
                //var script = "document.body.style.background = '#333333'";
            ChromiumSHARP.ExecuteScriptAsync(script);
            }


Image : http://i.imgur.com/RxvbsKx.png

But this Script don't work

I want run a script that allows me to add a custom css
That Either create a <style> </ style>, editing classes div's,
or adding a link referrer to file.css

Thank in advance.
Last edited by Shirotzu on Fri Sep 18, 2015 11:38 pm, edited 2 times in total.
Shirotzu
Mentor
 
Posts: 53
Joined: Fri Jun 05, 2015 9:35 pm

Re: CEF# ExecuteScriptAsync cannot execute a large script?

Postby amaitland » Sat Sep 12, 2015 7:22 pm

Use the DevTools to make sure your js is correct.

I'd typically use an anonymous closure for this type of code.
Maintainer of the CefSharp project.
amaitland
Virtuoso
 
Posts: 1292
Joined: Wed Jan 14, 2015 2:35 am

Re: CEF# ExecuteScriptAsync cannot execute a large script?

Postby Shirotzu » Sun Sep 13, 2015 2:49 pm

amaitland wrote:I'd typically use an anonymous closure for this type of code.


I'm a little confused about the anonymous closure,
although I think is a great idea use it, but i not know how to convert my code in anonymous closure

I saw a few examples but do not understand much that is what I did

http://prntscr.com/8fywwp
Code: Select all
                string loadjscssfile = "(function(filename, filetype){" +
                    @"if (filetype==""css""){" +
                    @"var fileref=document.createElement(""link"")" +
                    @"fileref.setAttribute(""rel"", ""stylesheet"")" +
                    @"fileref.setAttribute(""type"", ""text/css"")" +
                    @"fileref.setAttribute(""href"", filename)" + "}" +
                    @"if (typeof fileref!=""undefined"")" +
                    @"document.getElementsByTagName(""head"")[0].appendChild(fileref)" +
                    @"}" +
                    @"loadjscssfile(""http://trafficmonsoon.byethost32.com/script.css"", ""css""))();"; //This code add a css line in Head
                webBrowser.ExecuteScriptAsync(loadjscssfile);


You could transform my code to an anonymous closure, please?
Already I've tested the script code in a page and works perfectly

The script is from this website : http://www.javascriptkit.com/javatutors ... tcss.shtml

Thank in advance
Shirotzu
Mentor
 
Posts: 53
Joined: Fri Jun 05, 2015 9:35 pm

Re: CEF# ExecuteScriptAsync cannot execute a large script?

Postby amaitland » Sun Sep 13, 2015 8:04 pm

Shirotzu wrote:You could transform my code to an anonymous closure, please?


It's not that hard, you can work it out.

Shirotzu wrote:Already I've tested the script code in a page and works perfectly


Not the same, you should test using the page you plan on injecting the code into, make sure it works in context.

I don't believe your anywhere near reaching the limit in terms of script size, so likely something else causing your problem.
Maintainer of the CefSharp project.
amaitland
Virtuoso
 
Posts: 1292
Joined: Wed Jan 14, 2015 2:35 am

Re: CEF# ExecuteScriptAsync cannot execute a large script?

Postby Shirotzu » Sun Sep 13, 2015 9:18 pm

amaitland wrote:It's not that hard, you can work it out.


For me if it's hard because I do not know the closure anonymous

amaitland wrote:Not the same, you should test using the page you plan on injecting the code into, make sure it works in context.

I don't believe your anywhere near reaching the limit in terms of script size, so likely something else causing your problem.


I will try to see how open the DevTools to see what is failing, I want to run the script on the following page: https://www.messenger.com/
Shirotzu
Mentor
 
Posts: 53
Joined: Fri Jun 05, 2015 9:35 pm

Re: CEF# ExecuteScriptAsync cannot execute a large script?

Postby Shirotzu » Fri Sep 18, 2015 11:37 pm

I changed my script for one that make a <style> tag with the css code's
Code: Select all
                string script = "var css = '" +
                    ".body{background-color:red!important;} " + //A line of style
                    "'," +
                    "head = document.head || document.getElementsByTagName('head')[0]," +
                    "style = document.createElement('style');" +
                    "" +
                    "style.type = 'text/css';" +
                    "if (style.styleSheet){" +
                    "style.styleSheet.cssText = css;" +
                    "} else {" +
                    "style.appendChild(document.createTextNode(css));"+
                    "}" +
                    "head.appendChild(style);";

                browser.ExecuteScriptAsync(script);
Shirotzu
Mentor
 
Posts: 53
Joined: Fri Jun 05, 2015 9:35 pm


Return to Support Forum

Who is online

Users browsing this forum: No registered users and 88 guests

cron