Reputation: 11
so I am writing a radio station app that dynamically loads ads. The original solution was to go to https://cdn.azureradio.com/invocation/clearwater/iOS/the_beat.html in a WebView and then I used Swift Soup and souped the webview for the variables I needed. However, I recently found out that Web Views do not load requests in the background, IE: if the device is locked.
This obviously poses an issue. With that said, I decided on making an HTTP request. I called the url and got a Javascript string. I need to know of a way to run the javascript code and put the html that it generates into a variable so that I can soup the variable. Currently, the Javascript code that I get is
<!-- Azure Spot Server Javascript Tag - Generated with Revive Adserver v5.0.5 -->
<script type='text/javascript'><!--//<![CDATA[
var m3_u = (location.protocol=='https:'?'https://azureradio.com/ads/www/delivery/ajs.php':'http://azureradio.com/ads/www/delivery/ajs.php');
var m3_r = Math.floor(Math.random()*99999999999);
if (!document.MAX_used) document.MAX_used = ',';
document.write ("<scr"+"ipt type='text/javascript' src='"+m3_u);
document.write ("?zoneid=7&target=_top");
document.write ('&cb=' + m3_r);
if (document.MAX_used != ',') document.write ("&exclude=" + document.MAX_used);
document.write (document.charset ? '&charset='+document.charset : (document.characterSet ? '&charset='+document.characterSet : ''));
document.write ("&loc=" + escape(window.location));
if (document.referrer) document.write ("&referer=" + escape(document.referrer));
if (document.context) document.write ("&context=" + escape(document.context));
if (document.mmm_fo) document.write ("&mmm_fo=1");
document.write ("'><\/scr"+"ipt>");
//]]>--></script>
I have no idea how to run this code in swift to generate a URL without using webView.evaluateJavaScript("document.documentElement.outerHTML.toString()")
Thank you very much.
Upvotes: 0
Views: 2315
Reputation: 2051
You can use JavaScriptCore, which is a way to directly access WebKit's JavaScript engine without actually using an instance of a web view.
import JavaScriptCore
let context = JSContext()!
let value = context.evaluateScript(yourJavaScriptString)
print(value)
The value
that is returned is the value of the last element that was evaluated.
Upvotes: 3