15

Lion 用のアプリを開発しています。やりたいことは、.webarchive ファイルを開き、DOM のスニペットを変更して、変更した DOM を同じファイルに書き出すことです。

これまでの私のコードは次のとおりです。Web アーカイブを開き、変更してから、ファイルに保存します。

    NSString *archivePath = @"/Users/tigger/Library/Mail/V2/MailData/Signatures/1216DD8D-C7E2-4DE1-9FCD-0A9A3412C788.webarchive";
    NSData *plistData = [NSData dataWithContentsOfFile:archivePath];
    NSString *error;
    NSPropertyListFormat format;
    NSMutableDictionary *plist;

    plist = (NSMutableDictionary *)[NSPropertyListSerialization propertyListFromData:plistData
                                             mutabilityOption:NSPropertyListMutableContainersAndLeaves
                                                       format:&format
                                             errorDescription:&error];
    if(!plist){
        printf("no plist");
        [error release];
    }else{
        NSString *s = [NSString stringWithUTF8String:[[[plist objectForKey:@"WebMainResource"] objectForKey:@"WebResourceData"] bytes]];
        NSString *new = [s stringByReplacingOccurrencesOfString:@"</body>" withString:@"hey there!</body>"];

        [[plist objectForKey:@"WebMainResource"] setObject:new forKey:@"WebResourceData"];
        printf("Archive: %s", [[plist description] UTF8String]);       
        NSData *data = [NSPropertyListSerialization dataFromPropertyList:plist format:NSPropertyListBinaryFormat_v1_0 errorDescription:nil];
        [data writeToURL:[NSURL fileURLWithPath:@"/Users/tigger/Library/Mail/V2/MailData/Signatures/test.webarchive"] atomically:YES];

    }

問題は、結果の webarchive が無効であることです。オリジナルは次のようになります。

bplist00—_WebMainResource’  
_WebResourceTextEncodingName_WebResourceFrameName^WebResourceURL_WebResourceData_WebResourceMIMETypeUUTF-8PUdata:O<span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>Dan Shipper</div><div>dshipper@gmail.com</div><div><br></div></body></span><br class="Apple-interchange-newline">Ytext/html(F]l~îöõ°™
¥

結果の webarchive は次のようになります。

bplist00—_WebMainResource’  
^WebResourceURL_WebResourceFrameName_WebResourceMIMEType_WebResourceData_WebResourceTextEncodingNameUdata:PYtext/html_<span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; font-size: medium; "><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>Dan Shipper</div><div>dshipper@gmail.com</div><div><br></div>hey there!</body></span><br class="Apple-interchange-newline">UUTF-8(7Ndvîöõ•∏
æ

なぜ無効なのか、それを修正する方法について何か考えがありますか? 助けてくれてどうもありがとう!

また、textutil convert コマンドを使用して webarchive を生成しようとしましたが、元の HTML ファイルに次のような画像があるため、機能しません。

<img src="http://www.domainpolish.com/images/crowd.png">

しかし、textutil を使用すると、画像がダウンロードされ、次のように保存されます。

<img src"file:///1.png">

ダウンロードしたり、URLを変更したりしたくありませんが。noload、nostore、および baseurl オプションを使用しましたが、役に立ちませんでした。

編集:修正しました!! したがって、問題は、HTML を置き換えるときに、NSData ではなく NSString として挿入していたことです。

NSString *s = [NSString stringWithUTF8String:[[[plist objectForKey:@"WebMainResource"] objectForKey:@"WebResourceData"] bytes]];
NSString *new = [s stringByReplacingOccurrencesOfString:@"</body>" withString:@"hi there!</body>"];
NSData *sourceData = [new dataUsingEncoding:NSUTF8StringEncoding];
[[plist objectForKey:@"WebMainResource"] setObject:sourceData forKey:@"WebResourceData"];
4

2 に答える 2

1

ウィキペディアから:

webarchive 形式は、NSKeyedEncoder を使用してバイナリ plist 形式で保存されたファイル名を持つソース ファイルの連結です。

それを念頭に置いて、NSKeyedEncoderファイルのリストを見つけてから NSData を使用してファイルを分割し、探しているものを見つけることがHTMLできます。

于 2011-10-27T14:20:49.537 に答える