CefRegisterExtension not working

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.

CefRegisterExtension not working

Postby frontier » Mon Apr 27, 2015 3:09 am

Hello!

I wrote js extension code and registered with CefRegisterExtension.
js code like this.
Code: Select all
var scriptRemover;
if (!scriptRemover) scriptRemover = {};
(function() {
  scriptRemover.do = function() {
    var scripts = document.getElementsByTagName('script');
    if (!scripts) return;
    for (var k = scripts.length - 1; k >= 0; k--) {
      var item = scripts[k];
      item.parentNode.removeChild(item);
    }
    return true;
  };
})();

CefRegisterExtension("v8/scriptremover", code, NULL);


And run extension code in OnContextCreated callback like this.
Code: Select all
frame->ExecuteJavaScript(CefString("scriptRemover.do();"), "", 0);


After I build application and run it, I found that all scripts excuted despite of my extension code.
How can I remove <script> tags before they excuted?
frontier
Techie
 
Posts: 16
Joined: Sun Mar 15, 2015 9:30 pm

Re: CefRegisterExtension not working

Postby magreenblatt » Wed Apr 29, 2015 1:21 pm

What OS and CEF version? Where are you calling CefRegisterExtension from?
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: CefRegisterExtension not working

Postby frontier » Mon May 04, 2015 12:17 am

I use Centos 7 and CEF3 build 2171.
And the CefRegisterExtension function call located at OnWebKitInitialized callback.
frontier
Techie
 
Posts: 16
Joined: Sun Mar 15, 2015 9:30 pm

Re: CefRegisterExtension not working

Postby magreenblatt » Mon May 04, 2015 11:56 am

Try passing the frame URL to the ExecuteJavaScript call.
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: CefRegisterExtension not working

Postby frontier » Wed May 06, 2015 12:47 am

Code: Select all
frame->ExecuteJavaScript(CefString("scriptRemover.do();"), frame->GetURL(), 0);

This code not helps.

Below is my test routine.
1. Create test html document
Code: Select all
<html>
<head>
   <script>
      console.log("HEAD");
   </script>
</head>
<body>
   <script>
      console.log("BODY");
   </script>
</body>
</html>


2. Run CEF Application
3. RegisterExtension
4. Load html document
5. Run Extension code at OnContextCreated
6. If extension works successfully, cef console should not print "HEAD". But it shouldn't.

Is there any fault in my idea?
frontier
Techie
 
Posts: 16
Joined: Sun Mar 15, 2015 9:30 pm

Re: CefRegisterExtension not working

Postby magreenblatt » Wed May 06, 2015 9:18 am

The DOM has not been loaded at the time that OnContextCreated is called, which is likely why your code doesn't work. The document element creation event proposed in https://bitbucket.org/chromiumembedded/cef/issue/1454 might work for your use case. It has not yet been implemented.
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm


Return to Support Forum

Who is online

Users browsing this forum: Google [Bot] and 30 guests