2011-03-07 5 views
0

.webarchive 파일에 대한 액세스 권한이 있습니다. 필자는 파일에서 웹 아카이브 (PyObjC 사용)를 작성했습니다. DOM 트리의 일부 요소를 수정하고 수정 된 데이터를 출력하고 싶습니다.코코아에서 .webarchive를 수정하고 다시 작성하십시오.

WebArchive를 사용하면 일부 루트 DOM 트리 (webarchive는 링크가없는 하나의 웹 페이지)에 액세스해야한다고 생각합니다.

누구나 코코아에서이 작업을 수행 할 생각이 있습니까? (나는 PyObjC 잘 알고 아니에요) 당신이

답변

0

가능한 해결 방법 웹보기에 WebArchive를로드 할 코드가 제대로 표시

from Foundation import * 
import objc 
import WebKit 
from WebKit import * 
d=NSData.dataWithContentsOfFile_("/tmp/x.webarchive") 
ws=WebArchive.alloc().initWithData_(d) 
wv=WebView.alloc().initWithFrame_frameName_groupName_(((100, 100),(100,100)), "foo",None) 
mf=wv.mainFrame() 
mf.loadArchive_(ws) 
0

(아직까지는 확인되지 않은) 감사합니다. WebKit API (documentation)의 메소드를 사용하면 DOM을 쉽게 수정할 수 있습니다. 까다로운 점은 DOM을 수정하고 WebArchive에 수정 사항을 다시 쓰려는 것입니다. 새 WebArchive를 저장하는 것만으로는 수정 내용이 보존되지 않으므로 작동하지 않습니다. 따라서 새 소스를 작성해야합니다. 여기에 그렇게 할 몇 가지 코드 (여기에 웹보기가 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]; 

이이 때문에 해킹의 약간이다 문서화되지 않은 아카이브 형식의 내부 구조에 의존하지만, 나는 그것이 크게 변하지 않을 것이라고 꽤 안전하게 추측 할 수 있다고 생각합니다.

+0

좋습니다. 감사. – Jaycee

관련 문제