Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

HTML from Clipboard in Windows

by kean (Sexton)
on Aug 09, 2017 at 13:18 UTC ( [id://1197101]=perlquestion: print w/replies, xml ) Need Help??

kean has asked for the wisdom of the Perl Monks concerning the following question:

When i mark a table in any browser and copy these (in windows), in the clipboard are several formats included a HTML (CF_HTML) one. Is there a way to get the content of the clipboard with the HTML tags in perl? I tried Win32::Clipboard but i only get the text without html tags.

Replies are listed 'Best First'.
Re: HTML from Clipboard in Windows
by Jenda (Abbot) on Aug 09, 2017 at 14:44 UTC
    use Win32::Clipboard; my @arr = Win32::Clipboard::EnumFormats(); print "@arr\n"; foreach my $format (@arr) { print "Format: $format\n"; print Win32::Clipboard::GetAs($format); print "\n\n"; }

    This shows all available formats and their values. Looks like CF_HTML is 49485 and therefore to get the HTML version of the text in clipgoard you'd use Win32::Clipboard::GetAs(49485).

    Not sure why isn't the HTML included in the list of exported constants.

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

      I think it is because CF_HTML is a custom format and you are supposed to register it by name instead of a magic number.

      I've posted some code that does the lookup by name, but your solution is so much nicer and shorter.

      Update: GetClipboardFormatName and EnumClipboardFormats, which I interpret to that regard.

Re: HTML from Clipboard in Windows
by Corion (Patriarch) on Aug 09, 2017 at 15:04 UTC

    It doesn't seem to be the case currently.

    Win32::Clipboard only supports a fixed set of constants and does not pass other formats to the Win32 API.

    See this bug report, made just now.

    As a fun exercise, I wrote the following:

    #!perl -w use strict; use Win32::API; use Win32::Clipboard; use 5.006; our $registerClipboardFormat = Win32::API::More->new( 'user32', 'RegisterClipboardFormat', 'P', 'I' ); die "$^E" unless $registerClipboardFormat; #int WINAPI GetClipboardFormatName( # _In_ UINT format, # _Out_ LPTSTR lpszFormatName, # _In_ int cchMaxCount #); our $_getClipboardFormatName = Win32::API::More->new( 'user32', 'GetClipboardFormatName', 'IPI', 'I' ); die "$^E" unless $_getClipboardFormatName; sub getClipboardFormatName { my( $format ) = @_; my $buffer = "\0" x 1024; my $addr = unpack 'I', pack 'P', $buffer; #print $addr; warn $format; return "<builtin format>" if $format < 17; # CF_MAX $_getClipboardFormatName->Call( $format, $buffer, length $buffer ) or die $^E; $buffer =~ s/\0+$//; $buffer } our $CF_HTML; sub CF_HTML() { $CF_HTML ||= do { $registerClipboardFormat->Call("HTML Format"); }; } my $cb = Win32::Clipboard->new(); for my $fmt ($cb->EnumFormats()) { print getClipboardFormatName( $fmt ), "\n"; }; warn "Retrieving as " . CF_HTML(); print $cb->GetAs(CF_HTML()); print "\ndone\n";

    This code lists the names and also fetches the custom formats. I plan to do this as a patch to Win32::Clipboard, but currently don't have the time, sorry.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1197101]
Approved by Sinistral
Front-paged by Sinistral
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-03-29 07:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found