Search code examples
htmlobjective-cxcodesrcquicklook

How to open/link external files inside a HTML-designed QuickLook plugin without using the <src> attribute?


After my previous question, it seems that I gotta definitely deal with the fact that I have to use HTML in order to design interactive GUIs...but now the problem is another one: I know that for security reasons is not possible (unlike on Xcode 4.2 with OSX 10.6.8) to open/link anymore files from external directories using the <src> attribute, and I was wondering if there might be other ways to achieve that goal.

On this page ("Generating Enriched HTML" paragraph) is shown a portion of code that includes a CSS file as a MIME attachment: do you recon that it could be possible to obtain the same result with a different type of file (such a JS library or an image/video/audio)?

Here's some code from my project:

OSStatus GeneratePreviewForURL(void *thisInterface, QLPreviewRequestRef preview, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options)
{
    @autoreleasepool {

        if (QLPreviewRequestIsCancelled(preview)) return noErr;

        NSMutableString *html=[[NSMutableString alloc] init];
        NSDictionary *props;

        props=@{
            (__bridge NSString *)kQLPreviewPropertyTextEncodingNameKey:@"UTF-8",
            (__bridge NSString *)kQLPreviewPropertyMIMETypeKey:@"text/html",
            };

        [html appendString:@"<html>"];
        [html appendString:@"<head>"];
        [html appendString:@"<script type=\"text/javascript\" src=\"JQuery.js\">"];
        [html appendString:@"</script>"];
        [html appendString:@"<script>"];
        //...
        [html appendString:@"</script>"];
        [html appendString:@"</head>"];
        [html appendString:@"<body>"];
        //...
        [html appendString:@"</body>"];
        [html appendString:@"</html>"];

        QLPreviewRequestSetDataRepresentation(preview,(CFDataRef)[html dataUsingEncoding:NSUTF8StringEncoding],kUTTypeHTML,(CFDictionaryRef)props);
    }

    return noErr;
}

Thank you so much in advance!


Solution

  • Yes, all kinds of files (within the limits of how Quick Look sandboxes WebKit) can be attached using the cid: scheme. This is just a way to tell WebKit how to locate the data corresponding to a resource, so it can be used for all kinds of resources.

    In your case, load JQuery.js in a NSData object, write src=\"cid:JQuery.js\" and add this to your props object for the kQLPreviewPropertyAttachmentsKey

    @{
      @"JQuery.js" : @{ 
                 (__bridge NSString *)kQLPreviewPropertyMIMETypeKey : @"text/javascript",
                 (__bridge NSString *)kQLPreviewPropertyAttachmentDataKey: dataContainingJQuery
      },
    },