I've been experimenting with a proof of concept feature to implement the ability to conceal certain parts of a web page loaded in a webview, but I seem to be encountering some issues...
Within a UIWebview extension, I have something similar to this code snippet which is executed when the webview finishes loading:
let dummyStyle = "var dummyStyle = document.createElement('style'); dummyStyle.innerHTML = 'div {display: none;}'; document.body.appendChild(dummyStyle); "
let classToHide = "content"
let jsHideString = "var e = document.body.getElementByClassName('\(classToHide)'); e.style = dummyStyle; e.style.display = 'none';"
self.stringByEvaluatingJavaScriptFromString(dummyStyle + jsHideString)
The main issue appears to be (as determined using safari/chrome developer tools) that the document element does not possess a style property. Even if I manually assign it via console, it fails to update when set to e.style.display = 'none'.
In addition to locating the element by id or class, I aim to limit the assumptions made regarding the end user's web page.
Thank you for taking the time to read my inquiry!
Edit with operational solution:
let classToHide = "content"
let jsHideString = " " +
" var e = document.body.getElementsByClassName(\"\(classToHide)\")[0];" +
"e.style.display = \"none\";"
let DOMContentLoadedNotification = " " +
"var addL = function addListener(obj, eventName, listener) { " +
"if (obj.addEventListener) { " +
"alert('added listener');" +
"obj.addEventListener(eventName, listener, false); " +
"} else { " +
"alert('attactch event');" +
"obj.attachEvent(\"on\" + eventName, listener); " +
"};" +
"};" +
"var completion = function finishedDCL() { " +
"alert('finishedDCL');" +
jsHideString +
"};" +
"if (document.readyState == \"complete\" || document.readyState == \"loaded\") { " +
"alert('document already loaded');" +
jsHideString +
"} else {" +
"alert('document not loaded');" +
"addL(document, \"DOMContentLoaded\", completion()); " +
"};"
print("Webview: \(self.stringByEvaluatingJavaScriptFromString(DOMContentLoadedNotification))")