How can I access full source of a WebView

  • Hi

    I am trying to send the contents of a webwiew with email. I have found
    several ways to do that, but there are minor problems.

    //    get the attributes string of the webview
    id res = (id <WebDocumentText> )[[[myWebView mainFrame] frameView]
    documentView];
    NSAttributedString *p = [res attributedString];
    NSPasteboard *pb = [NSPasteboard pasteboardWithName:@"PBtoMail"];

    //    init a private pastebord for data transfer, this time use
    webarchive for transfer
    [pb declareTypes:[NSArray arrayWithObject:NSRTFDPboardType]
    owner:self];

    //    convert the attributed string to rtfd and place it on the private
    pasteboard
    NSRange range;
    range.location = 0;
    range.length = [p length];
    [pb setData:(NSData *)[p RTFDFromRange:range documentAttributes:nil
    forType:NSRTFDPboardType];

    //    open mail window using the rtfd data using services...
    NSPerformService(@"Mail/Send Selection", pb);

    This works, but quality is no so good. So I tried a different approach

    //    init a private pastebord for data transfer, this time use
    webarchive for transfer
    NSPasteboard *pb = [NSPasteboard pasteboardWithName:@"PBtoMail"];
    [pb declareTypes:[NSArray arrayWithObject:WebArchivePboardType]
    owner:self];

    //    get the webarchive and place it on pboard
    [pb setData:[[[myWebView mainFrameDocument] webArchive] data]
    forType:WebArchivePboardType];

    NSPerformService(@"Mail/Send Selection", pb);

    This works better. However external CSS code is not copied, which
    makes the page look different. When I select all and copy from the
    webview manualy and then paste it on a new mail window it works fine,
    the external CSS is copied as well. Is there any way I get the full
    source (including the external CSS) so that I can place it on the
    pasteboard?

    Thanks in advance.
  • On 08/10/2008, at 8:45 AM, Ömer KardaÅŸ wrote:

    > This works better. However external CSS code is not copied, which
    > makes the page look different. When I select all and copy from the
    > webview manualy and then paste it on a new mail window it works
    > fine, the external CSS is copied as well. Is there any way I get the
    > full source (including the external CSS) so that I can place it on
    > the pasteboard?

    In order to get the source of a WebView you'd do something like this:

    DOMHTMLElement* el=(DOMHTMLElement*)[self
    mainFrameDocument].documentElement;
    NSString* source=el.outerHTML;

    However, external CSS files are not part of the source of the page so
    no method of obtaining just the source code will do this for you.
    I suspect what is happening when you copy and paste into Mail is that
    the WebView is placing a web archive on the clipboard, which contains
    all the images and other linked resources.

    You can get a web archive like this:

    [[[webView mainFrame] dataSource] webArchive];

    When copying to the clipboard you can specify the type as
    WebArchivePboardType.

    --
    Rob Keniger
  • Thanks Rob,

    I tried your way (DOMHTMLElement* el...) and it gave me the source.
    Unfortunatly it gives only source and does not include external files.

    I already tried your second approach.
    [pb setData:[[[myWebView mainFrameDocument] webArchive] data]
    forType:WebArchivePboardType];

    I just found something intereting. I was wrong to think that the
    external CSS is not copied in this way. I saved the Pasteboard as
    webarchive, opened it from Safari, and looked at the source. CSS was
    not there. On the contrary, webarchive produced by manual copy had the
    CSS code. But then, I opened the webarhives as TEXT (using
    subethaedit). The CSS code was PRESENT in BOTH of the WebArchive
    files. Somehow Safari can not see the one produced by the above code.

    Any other suggestions?

    On Oct 8, 2008, at 2:13 AM, Rob Keniger wrote:

    >
    > On 08/10/2008, at 8:45 AM, Ömer Kardaş wrote:
    >
    >> This works better. However external CSS code is not copied, which
    >> makes the page look different. When I select all and copy from the
    >> webview manualy and then paste it on a new mail window it works
    >> fine, the external CSS is copied as well. Is there any way I get
    >> the full source (including the external CSS) so that I can place it
    >> on the pasteboard?
    >
    >
    > In order to get the source of a WebView you'd do something like this:
    >
    > DOMHTMLElement* el=(DOMHTMLElement*)[self
    > mainFrameDocument].documentElement;
    > NSString* source=el.outerHTML;
    >
    > However, external CSS files are not part of the source of the page
    > so no method of obtaining just the source code will do this for you.
    > I suspect what is happening when you copy and paste into Mail is
    > that the WebView is placing a web archive on the clipboard, which
    > contains all the images and other linked resources.
    >
    > You can get a web archive like this:
    >
    > [[[webView mainFrame] dataSource] webArchive];
    >
    > When copying to the clipboard you can specify the type as
    > WebArchivePboardType.
    >
    > --
    > Rob Keniger