WebArchiveをWebViewにロードするコードは正しいように見えます(私はPyObjCにあまり詳しくありません)。DOMの変更は、WebKit API(ドキュメント)のメソッドを使用して非常に簡単です。トリッキーな点は、DOMを変更し、その変更をWebArchiveに書き戻したい場合です。新しいWebArchiveを保存するだけでは、変更が保持されないため機能しません。そのため、新しいソースを作成する必要があります。これを行うコードを次に示します(ここでは、WebViewがwebview
あり、元のWevArchiveはarchivePathにあり、変更されたバージョンもそこに書き込まれます)。
//Get the string representation of the current DOM tree
NSString *sourceString = [(DOMHTMLElement *)[[[webview mainFrame] DOMDocument] documentElement] outerHTML];
NSData *sourceData = [sourceString dataUsingEncoding:NSUTF8StringEncoding];
//Load the archive from disk to a dictionary (it's a plist)
NSMutableDictionary *archive = [[NSMutableDictionary alloc] initWithContentsOfURL:[NSURL fileURLWithPath:archivePath]];
//Modify the main HTML
[(NSMutableDictionary *)[archive objectForKey:@"WebMainResource"] setObject:sourceData forKey:@"WebResourceData"];
//Write the plist back out
NSData *data = [NSPropertyListSerialization dataFromPropertyList:archive format:NSPropertyListBinaryFormat_v1_0 errorDescription:nil];
[data writeToURL:[NSURL fileURLWithPath:ArchivePath] atomically:YES];
文書化されていないアーカイブ形式の内部構造に依存しているため、これは少しハックですが、大幅に変更されることはないとかなり安全に想定できると思います。