Page 1 of 1

CefRegisterExtension not working

PostPosted: Mon Apr 27, 2015 3:09 am
by frontier
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?

Re: CefRegisterExtension not working

PostPosted: Wed Apr 29, 2015 1:21 pm
by magreenblatt
What OS and CEF version? Where are you calling CefRegisterExtension from?

Re: CefRegisterExtension not working

PostPosted: Mon May 04, 2015 12:17 am
by frontier
I use Centos 7 and CEF3 build 2171.
And the CefRegisterExtension function call located at OnWebKitInitialized callback.

Re: CefRegisterExtension not working

PostPosted: Mon May 04, 2015 11:56 am
by magreenblatt
Try passing the frame URL to the ExecuteJavaScript call.

Re: CefRegisterExtension not working

PostPosted: Wed May 06, 2015 12:47 am
by frontier
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?

Re: CefRegisterExtension not working

PostPosted: Wed May 06, 2015 9:18 am
by magreenblatt
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.