nirvana74v
nirvana74v

Reputation: 1091

Accessing DOM object model JavascriptCore

I have a code where I need to access DOM object model in my Objective-C code. I am trying to execute a function that is suppose to return the content

    NSString *functionScr = [NSString stringWithFormat: @"var fPreset=\"%@\";"
"var addFavorite = function() {"
"   if (fPreset != '') {"
"        var ft = window.document.createElement('div');"
"        ft.innerHTML =  decodeURIComponent(fPreset);"
"        var f = ft.innerHTML;"
"        var bub = encodeURIComponent(f.childNodes[1]);"
"        return bub;"
"       }"
"   }",encodedHTML];

[context evaluateScript:functionScr];
JSValue *retVAL = [context evaluateScript:@"addFavorite();"];

when this executes I get an error

ReferenceError: Can't find variable: window

How can I access window object in JavascriptCore?

Upvotes: 4

Views: 1332

Answers (2)

ssgxx
ssgxx

Reputation: 79

This works for me

JSContext *context = [yourwebview valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
[context evaluateScript:yourfunctionScr];
JSValue *retVAL = [context evaluateScript:@"addFavorite();"];

Upvotes: 1

Kostiantyn Koval
Kostiantyn Koval

Reputation: 8483

It's is not possible :( The DOM and any other window property is not available in the JavascriptCore. It is a pure JS engine

Upvotes: 4

Related Questions