Ios WKWebView UITableView单元格高度和Instagram嵌入代码

Ios WKWebView UITableView单元格高度和Instagram嵌入代码,ios,objective-c,uitableview,instagram,wkwebview,Ios,Objective C,Uitableview,Instagram,Wkwebview,我试图在表格视图中将Instagram帖子添加到单元格中,但我无法正确计算单元格高度,尽管我尝试了许多不同的方法。我正在为每个单元格创建一个WKWebView,并使用loadHTMLString和Instagram提供的嵌入代码,对Instagram的HTML稍作修改如下: `<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"></head>` Ins

我试图在表格视图中将Instagram帖子添加到单元格中,但我无法正确计算单元格高度,尽管我尝试了许多不同的方法。我正在为每个单元格创建一个
WKWebView
,并使用
loadHTMLString
和Instagram提供的嵌入代码,对Instagram的HTML稍作修改如下:

`<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"></head>`
InstagramViewController.m

#import "InstagramViewController.h"

@interface InstagramViewController ()
@property (nonatomic) BOOL loaded;
@property (nonatomic) CGFloat cellHeight;

@end

@implementation InstagramViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.estimatedRowHeight = 150;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *simpleTableIdentifier = @"simpleTableIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
    WKWebView *webView = [[WKWebView alloc] initWithFrame:cell.contentView.frame configuration:theConfiguration];
    webView.navigationDelegate = self;
    [webView.scrollView setScrollEnabled:NO];
    webView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    webView.tag = indexPath.section;
    NSString *instagramEmbedHTML = @"\
    <!DOCTYPE html>\
    <html>\
    <head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\
    </head>\
    <body>\
    <blockquote class=\"instagram-media\" data-instgrm-captioned data-instgrm-version=\"7\" style=\" background:#FFF; border:0; border-radius:3px; box-shadow:0 0 1px 0 rgba(0,0,0,0.5),0 1px 10px 0 rgba(0,0,0,0.15); margin: 1px; max-width:658px; padding:0; width:99.375%; width:-webkit-calc(100% - 2px); width:calc(100% - 2px);\">\
    <div style=\"padding:8px;\">\
    <div style=\" background:#F8F8F8; line-height:0; margin-top:40px; padding:41.91489361702128% 0; text-align:center; width:100%;\">\
    <div style=\" background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACwAAAAsCAMAAAApWqozAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAMUExURczMzPf399fX1+bm5mzY9AMAAADiSURBVDjLvZXbEsMgCES5/P8/t9FuRVCRmU73JWlzosgSIIZURCjo/ad+EQJJB4Hv8BFt+IDpQoCx1wjOSBFhh2XssxEIYn3ulI/6MNReE07UIWJEv8UEOWDS88LY97kqyTliJKKtuYBbruAyVh5wOHiXmpi5we58Ek028czwyuQdLKPG1Bkb4NnM+VeAnfHqn1k4+GPT6uGQcvu2h2OVuIf/gWUFyy8OWEpdyZSa3aVCqpVoVvzZZ2VTnn2wU8qzVjDDetO90GSy9mVLqtgYSy231MxrY6I2gGqjrTY0L8fxCxfCBbhWrsYYAAAAAElFTkSuQmCC); display:block; height:44px; margin:0 auto -44px; position:relative; top:-22px; width:44px;\">\
    </div>\
    </div>\
    <p style=\" margin:8px 0 0 0; padding:0 4px;\">\
    <a href=\"https://www.instagram.com/p/BVR2uajF1Qc/\" style=\" color:#000; font-family:Arial,sans-serif; font-size:14px; font-style:normal; font-weight:normal; line-height:17px; text-decoration:none; word-wrap:break-word;\" target=\"_blank\">I wish for... free cupcakes!! I've been struggling with this for a very long time. This solution works for me.

You will get the appropriate height. From that point it is your task to handle it and set the container's height.

BE WARNED: It was not straightforward.

NOTE: Instagram and Twitter embed is a really awful animal here. Their script calculates the height for the width ot 621(!) and sets it as the attribute of the iframe. So you "simply" (LOL) have to recalculate the size. See the new attribute "modded", to avoid setting height multiple times.

NO CROSS DOMAIN restrictions. You cannot read/alter an iframe's content loaded from an url different from the baseURL-s domain. That would be too easy, but browsers deny this for a very good reason.

This is a mixed solution using HTML, CSS, Javascript and of course Swift.


func initMyWebView() {

//...

        let contentController = WKUserContentController()

        let config = WKWebViewConfiguration()
        config.preferences = preferences
        config.allowsInlineMediaPlayback = true
        config.userContentController = contentController
        config.mediaTypesRequiringUserActionForPlayback = []

        contentController.add(
            self,
            name: "heightChanged"
        )
        contentController.add(
            self,
            name: "log"
        )
        contentController.removeAllUserScripts()
        contentController.addUserScript(WKUserScript(source: windowSizeWatcherScript(), injectionTime: .atDocumentEnd, forMainFrameOnly: true))
// ...

}

    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        if message.name == "log" {
            print("JSLog: " + String(describing: message.body))
            return
        }

        if message.name == "heightChanged" {
            if let messageDict = message.body as? Dictionary<String, CGFloat> {
                if nil != messageDict["height"] {
                    DispatchQueue.main.async { [weak self] in
                        // Implement the height update to your needs
                        self?.updateHeight(height: messageDict["height"]!)
                    }
                }
            }
            return
        }

    }


func style(jsonStyle: JSONTextStyleModel?) -> String {
        let maxHTMLW = bounds.width

        return """
        html, body {
            margin: 0 !important;
            padding: 0 !important;
            width: 100% !important;
            max-width: \(maxHTMLW)px !important;
            min-width: \(maxHTMLW)px !important;
        }
        body {
            font-family: Roboto;
            background-color: \(Theme.shared.generalColors.background);
            color: \((jsonStyle?.color).defaultsTo(value: "#333"));
            font-size: 16px;
            font-weight: 300;
        }

        #\(contentId) {
            margin: 0 2% !important;
            padding: 0 !important;
            height: 100%;
            display: block;
            width: 96%;
        }

        iframe {
            width: 100% !important;
        }

        iframe.instagram-media-rendered {
        }
        """
    }

    func online(html: String, with style: String) -> String {
        return """
        <!DOCTYPE html>
        <html>
            <head>
                <meta name="viewport" content="width=device-width, initial-scale=1">
                <style>
                \(style)
                </style>                
            </head>
            <body>
                <div id="\(contentId)">
                    \(html)
                </div>
            </body>
        </html>
        """
    }

 func windowSizeWatcherScript() -> String {
        let maxInstaW = bounds.width * 0.96
        return """
            var prevHeight = 0;
            var prevWidth = 0;
            var contentId = "\(contentId)";
            customLog("windowSizeWatcherScript added")

            var resizeTimeout = setTimeout(function () { resize(); }, 125);


            if (typeof(webkit) !== 'undefined' && typeof(webkit.messageHandlers.log) !== 'undefined') {
                console.log = customLog;
            }

            function resize() {
                clearTimeout(resizeTimeout);
                resizeTimeout = setTimeout('resize();', 250);

                if (typeof(webkit) == 'undefined' || typeof(webkit.messageHandlers.heightChanged) == 'undefined') {
                    customLog("Cannot send webkit messages");
                    return;
                }


                if (resizeInsta()) {
                    return
                }

                if (resizeTwitter()) {
                    return
                }

                if (resizeIframes()) {
                    return
                }

                if (resizeContent()) {
                    return
                }

                resizeDocument()

            }

            function customLog(message) {
                if (typeof(webkit) == "undefined" || typeof(webkit.messageHandlers.log) == "undefined") {
                    console.log("No native app context");
                    console.log(message)
                    return
                }
                try { webkit.messageHandlers.log.postMessage(message);} catch (err) { console.log("No native app context"); }
            }

            function handleSize(width, height) {
                if (height > 0 && prevHeight != height) {
                    customLog("W: " + width + " H: " + height);
                    try { webkit.messageHandlers.heightChanged.postMessage({"height": height});} catch (err) { customLog("No native app context"); }
                    customLog("Detected width " + width + " and height " + height);
                    prevHeight = height;
                }
            }

            function resizeInsta() {
                var insta = document.getElementsByClassName("instagram-media-rendered");

                if (typeof(insta) !== "undefined" && insta.length > 0) {
                    var width = insta[0].offsetWidth;
                    var height = Math.max(insta[0].scrollHeight, insta[0].offsetHeight);
                    var h = insta[0].getAttribute("height")
                    if (h !== "undefined" && h > 0) {
                        var modded = insta[0].getAttribute("modded")
                        if (modded == "undefined") {
                            h = h.replace("px", "")
                            let r = \(maxInstaW) / 621;
                            height = h * r;
                            width = \(Int(maxInstaW));
                            insta[0].setAttribute("height", height + "px");
                            insta[0].setAttribute("modded", "1");
                            insta[0].setAttribute("width", "\(Int(maxInstaW))px");
                        }
                    }
                    handleSize(width, height)
                    return true;
                }

                return false;
            }

            function resizeTwitter() {
                var insta = document.getElementsByClassName("twitter-tweet-rendered");

                if (typeof(insta) !== "undefined" && insta.length > 0) {
                    var width = insta[0].offsetWidth;
                    var height = Math.max(insta[0].scrollHeight, insta[0].offsetHeight);
                    var h = insta[0].getAttribute("height")
                    if (h !== "undefined" && h > 0) {
                        var modded = insta[0].getAttribute("modded")
                        if (modded == "undefined") {
                            h = h.replace("px", "")
                            let r = \(maxInstaW) / 621;
                            height = h * r;
                            width = \(Int(maxInstaW));
                            insta[0].setAttribute("height", height + "px");
                            insta[0].setAttribute("modded", "1");
                            insta[0].setAttribute("width", "\(Int(maxInstaW))px");
                        }
                    }
                    handleSize(width, height)
                    return true;
                }

                return false;
            }

            function resizeIframes() {
                var iframes = document.getElementsByTagName("iframe");

                if (typeof(iframes) !== "undefined" && iframes.length > 0) {
                    var width = iframes[0].offsetWidth;
                    var height = Math.max(iframes[0].scrollHeight, iframes[0].offsetHeight);
                    handleSize(width, height)
                    return true
                }
                return false
            }

            function resizeContent() {
                var content = document.getElementById(contentId);
                if (typeof(content) !== 'undefined') {
                        var width = content.offsetWidth;
                        var height = Math.max(content.scrollHeight, content.offsetHeight);
                        handleSize(width, height)
                        return true
                }
                return false
            }

        function resizeDocument() {
            var width = document.body.offsetWidth;
            var height = Math.max(document.body.scrollHeight, document.body.offsetHeight);
            handleSize(width, height)
            return true
        }
"""
    }
#导入“InstagramViewController.h”
@接口InstagramViewController()
@属性(非原子)布尔加载;
@属性(非原子)CGFloat单元高度;
@结束
@InstagramViewController的实现
-(无效)viewDidLoad{
[超级视图下载];
self.tableView.estimateDroweight=150;
self.tableView.rowHeight=UITableViewAutomaticDimension;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
返回2;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
静态NSString*simpleTableIdentifier=@“simpleTableIdentifier”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:simpleTableIdentifier];
}
WKWebViewConfiguration*配置=[[WKWebViewConfiguration alloc]init];
WKWebView*webView=[[WKWebView alloc]initWithFrame:cell.contentView.frame配置:配置];
webView.navigationDelegate=self;
[webView.scrollView设置可滚动:否];
webView.autoresizingMask=uiviewsautoresizingflexiblewhight | uiviewsautoresizingflexiblewidth;
webView.tag=indexPath.section;
NSString*instagramEmbedHTML=@”\
\
\
\
\
\
\
\
\
\
\
\

\


我希望…有免费的纸杯蛋糕!!我已经为此奋斗了很长时间。这个解决方案对我来说很有效

您将获得适当的高度。从这一点开始,您的任务就是处理它并设置容器的高度

请注意:这并不简单

注意:Instagram和Twitter embed在这里是一个非常糟糕的动物。他们的脚本计算宽度为621(!)的高度,并将其设置为iframe的属性。因此您“简单”(LOL)必须重新计算大小。请参阅新属性“Moded”,以避免多次设置高度

没有跨域限制。您无法读取/更改从不同于baseURL-s域的url加载的iframe内容。这太容易了,但浏览器拒绝这样做是有很好的理由的

这是一个混合解决方案,使用HTML、CSS、Javascript,当然还有Swift


func initMyWebView(){
//...
让contentController=WKUserContentController()
让config=WKWebViewConfiguration()
config.preferences=首选项
config.allowsInlineMediaPlayback=true
config.userContentController=contentController
config.mediaTypesRequiringUserActionForPlayback=[]
contentController.add(
自己
名称:“高度更改”
)
contentController.add(
自己
名称:“日志”
)
contentController.removeAllUserScripts()
contentController.addUserScript(WKUserScript(源代码:windowSizeWatcherScript(),注入时间:.atDocumentEnd,FormInfraMeOnly:true))
// ...
}
func userContentController(userContentController:WKUserContentController,didReceive消息:WKScriptMessage){
如果message.name==“日志”{
打印(“JSLog:+字符串(描述:message.body))
返回
}
如果message.name==“高度已更改”{
如果让messageDict=message.body作为字典{
如果为零!=messageDict[“高度”]{
DispatchQueue.main.async{[weak self]位于
//根据您的需要实施高度更新
self?.updateHeight(高度:messageDict[“height”]!)
}
}
}
返回
}
}
func样式(jsonStyle:JSONTextStyleModel?->String{
设maxHTMLW=bounds.width
返回“”
html,正文{
边距:0!重要;
填充:0!重要;
宽度:100%!重要;
最大宽度:\(maxHTMLW)px!重要;
最小宽度:\(maxHTMLW)px!重要;
}
身体{
字体系列:Roboto;
背景色:\(Theme.shared.generalColors.background);
颜色:\((jsonStyle?.color).defaultsTo(值:#333”);
字体大小:16px;
字体大小:300;
}
#\(contentId){
利润率:0.2%!重要;
填充:0!重要;
身高:100%;
显示:块;
宽度:96%;
}
iframe{
宽度:100%!重要;
}
iframe.instagram-media-rendered{
}
"""
}
func online(html:String,带样式:String)->String{
返回“”
\(风格)
\(html)
"""
}
func windowSizeWatcherScript()->字符串{
设maxInstaW=bounds.width*0.96
返回“”
var-prevHeight=0;
var-prevWidth=0;
var contentId=“\(contentId)”;
自定义日志(“添加了windowSizeWatcherScript”)
var resizeTimeout=setTimeout(函数(){resize();},125);
if(typeof(webkit)!='undefined'&&typeof(webkit.messageHandlers.log)!='undefined'){
console.log=customLog;
}
#import "InstagramViewController.h"

@interface InstagramViewController ()
@property (nonatomic) BOOL loaded;
@property (nonatomic) CGFloat cellHeight;

@end

@implementation InstagramViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.estimatedRowHeight = 150;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *simpleTableIdentifier = @"simpleTableIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
    WKWebView *webView = [[WKWebView alloc] initWithFrame:cell.contentView.frame configuration:theConfiguration];
    webView.navigationDelegate = self;
    [webView.scrollView setScrollEnabled:NO];
    webView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    webView.tag = indexPath.section;
    NSString *instagramEmbedHTML = @"\
    <!DOCTYPE html>\
    <html>\
    <head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\
    </head>\
    <body>\
    <blockquote class=\"instagram-media\" data-instgrm-captioned data-instgrm-version=\"7\" style=\" background:#FFF; border:0; border-radius:3px; box-shadow:0 0 1px 0 rgba(0,0,0,0.5),0 1px 10px 0 rgba(0,0,0,0.15); margin: 1px; max-width:658px; padding:0; width:99.375%; width:-webkit-calc(100% - 2px); width:calc(100% - 2px);\">\
    <div style=\"padding:8px;\">\
    <div style=\" background:#F8F8F8; line-height:0; margin-top:40px; padding:41.91489361702128% 0; text-align:center; width:100%;\">\
    <div style=\" background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACwAAAAsCAMAAAApWqozAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAMUExURczMzPf399fX1+bm5mzY9AMAAADiSURBVDjLvZXbEsMgCES5/P8/t9FuRVCRmU73JWlzosgSIIZURCjo/ad+EQJJB4Hv8BFt+IDpQoCx1wjOSBFhh2XssxEIYn3ulI/6MNReE07UIWJEv8UEOWDS88LY97kqyTliJKKtuYBbruAyVh5wOHiXmpi5we58Ek028czwyuQdLKPG1Bkb4NnM+VeAnfHqn1k4+GPT6uGQcvu2h2OVuIf/gWUFyy8OWEpdyZSa3aVCqpVoVvzZZ2VTnn2wU8qzVjDDetO90GSy9mVLqtgYSy231MxrY6I2gGqjrTY0L8fxCxfCBbhWrsYYAAAAAElFTkSuQmCC); display:block; height:44px; margin:0 auto -44px; position:relative; top:-22px; width:44px;\">\
    </div>\
    </div>\
    <p style=\" margin:8px 0 0 0; padding:0 4px;\">\
    <a href=\"https://www.instagram.com/p/BVR2uajF1Qc/\" style=\" color:#000; font-family:Arial,sans-serif; font-size:14px; font-style:normal; font-weight:normal; line-height:17px; text-decoration:none; word-wrap:break-word;\" target=\"_blank\">I wish for... free cupcakes!! I've been struggling with this for a very long time. This solution works for me.

You will get the appropriate height. From that point it is your task to handle it and set the container's height.

BE WARNED: It was not straightforward.

NOTE: Instagram and Twitter embed is a really awful animal here. Their script calculates the height for the width ot 621(!) and sets it as the attribute of the iframe. So you "simply" (LOL) have to recalculate the size. See the new attribute "modded", to avoid setting height multiple times.

NO CROSS DOMAIN restrictions. You cannot read/alter an iframe's content loaded from an url different from the baseURL-s domain. That would be too easy, but browsers deny this for a very good reason.

This is a mixed solution using HTML, CSS, Javascript and of course Swift.


func initMyWebView() {

//...

        let contentController = WKUserContentController()

        let config = WKWebViewConfiguration()
        config.preferences = preferences
        config.allowsInlineMediaPlayback = true
        config.userContentController = contentController
        config.mediaTypesRequiringUserActionForPlayback = []

        contentController.add(
            self,
            name: "heightChanged"
        )
        contentController.add(
            self,
            name: "log"
        )
        contentController.removeAllUserScripts()
        contentController.addUserScript(WKUserScript(source: windowSizeWatcherScript(), injectionTime: .atDocumentEnd, forMainFrameOnly: true))
// ...

}

    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        if message.name == "log" {
            print("JSLog: " + String(describing: message.body))
            return
        }

        if message.name == "heightChanged" {
            if let messageDict = message.body as? Dictionary<String, CGFloat> {
                if nil != messageDict["height"] {
                    DispatchQueue.main.async { [weak self] in
                        // Implement the height update to your needs
                        self?.updateHeight(height: messageDict["height"]!)
                    }
                }
            }
            return
        }

    }


func style(jsonStyle: JSONTextStyleModel?) -> String {
        let maxHTMLW = bounds.width

        return """
        html, body {
            margin: 0 !important;
            padding: 0 !important;
            width: 100% !important;
            max-width: \(maxHTMLW)px !important;
            min-width: \(maxHTMLW)px !important;
        }
        body {
            font-family: Roboto;
            background-color: \(Theme.shared.generalColors.background);
            color: \((jsonStyle?.color).defaultsTo(value: "#333"));
            font-size: 16px;
            font-weight: 300;
        }

        #\(contentId) {
            margin: 0 2% !important;
            padding: 0 !important;
            height: 100%;
            display: block;
            width: 96%;
        }

        iframe {
            width: 100% !important;
        }

        iframe.instagram-media-rendered {
        }
        """
    }

    func online(html: String, with style: String) -> String {
        return """
        <!DOCTYPE html>
        <html>
            <head>
                <meta name="viewport" content="width=device-width, initial-scale=1">
                <style>
                \(style)
                </style>                
            </head>
            <body>
                <div id="\(contentId)">
                    \(html)
                </div>
            </body>
        </html>
        """
    }

 func windowSizeWatcherScript() -> String {
        let maxInstaW = bounds.width * 0.96
        return """
            var prevHeight = 0;
            var prevWidth = 0;
            var contentId = "\(contentId)";
            customLog("windowSizeWatcherScript added")

            var resizeTimeout = setTimeout(function () { resize(); }, 125);


            if (typeof(webkit) !== 'undefined' && typeof(webkit.messageHandlers.log) !== 'undefined') {
                console.log = customLog;
            }

            function resize() {
                clearTimeout(resizeTimeout);
                resizeTimeout = setTimeout('resize();', 250);

                if (typeof(webkit) == 'undefined' || typeof(webkit.messageHandlers.heightChanged) == 'undefined') {
                    customLog("Cannot send webkit messages");
                    return;
                }


                if (resizeInsta()) {
                    return
                }

                if (resizeTwitter()) {
                    return
                }

                if (resizeIframes()) {
                    return
                }

                if (resizeContent()) {
                    return
                }

                resizeDocument()

            }

            function customLog(message) {
                if (typeof(webkit) == "undefined" || typeof(webkit.messageHandlers.log) == "undefined") {
                    console.log("No native app context");
                    console.log(message)
                    return
                }
                try { webkit.messageHandlers.log.postMessage(message);} catch (err) { console.log("No native app context"); }
            }

            function handleSize(width, height) {
                if (height > 0 && prevHeight != height) {
                    customLog("W: " + width + " H: " + height);
                    try { webkit.messageHandlers.heightChanged.postMessage({"height": height});} catch (err) { customLog("No native app context"); }
                    customLog("Detected width " + width + " and height " + height);
                    prevHeight = height;
                }
            }

            function resizeInsta() {
                var insta = document.getElementsByClassName("instagram-media-rendered");

                if (typeof(insta) !== "undefined" && insta.length > 0) {
                    var width = insta[0].offsetWidth;
                    var height = Math.max(insta[0].scrollHeight, insta[0].offsetHeight);
                    var h = insta[0].getAttribute("height")
                    if (h !== "undefined" && h > 0) {
                        var modded = insta[0].getAttribute("modded")
                        if (modded == "undefined") {
                            h = h.replace("px", "")
                            let r = \(maxInstaW) / 621;
                            height = h * r;
                            width = \(Int(maxInstaW));
                            insta[0].setAttribute("height", height + "px");
                            insta[0].setAttribute("modded", "1");
                            insta[0].setAttribute("width", "\(Int(maxInstaW))px");
                        }
                    }
                    handleSize(width, height)
                    return true;
                }

                return false;
            }

            function resizeTwitter() {
                var insta = document.getElementsByClassName("twitter-tweet-rendered");

                if (typeof(insta) !== "undefined" && insta.length > 0) {
                    var width = insta[0].offsetWidth;
                    var height = Math.max(insta[0].scrollHeight, insta[0].offsetHeight);
                    var h = insta[0].getAttribute("height")
                    if (h !== "undefined" && h > 0) {
                        var modded = insta[0].getAttribute("modded")
                        if (modded == "undefined") {
                            h = h.replace("px", "")
                            let r = \(maxInstaW) / 621;
                            height = h * r;
                            width = \(Int(maxInstaW));
                            insta[0].setAttribute("height", height + "px");
                            insta[0].setAttribute("modded", "1");
                            insta[0].setAttribute("width", "\(Int(maxInstaW))px");
                        }
                    }
                    handleSize(width, height)
                    return true;
                }

                return false;
            }

            function resizeIframes() {
                var iframes = document.getElementsByTagName("iframe");

                if (typeof(iframes) !== "undefined" && iframes.length > 0) {
                    var width = iframes[0].offsetWidth;
                    var height = Math.max(iframes[0].scrollHeight, iframes[0].offsetHeight);
                    handleSize(width, height)
                    return true
                }
                return false
            }

            function resizeContent() {
                var content = document.getElementById(contentId);
                if (typeof(content) !== 'undefined') {
                        var width = content.offsetWidth;
                        var height = Math.max(content.scrollHeight, content.offsetHeight);
                        handleSize(width, height)
                        return true
                }
                return false
            }

        function resizeDocument() {
            var width = document.body.offsetWidth;
            var height = Math.max(document.body.scrollHeight, document.body.offsetHeight);
            handleSize(width, height)
            return true
        }
"""
    }