{"id":453,"date":"2014-07-21T07:00:00","date_gmt":"2014-07-21T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2014\/07\/21\/how-can-i-get-the-url-to-the-web-page-the-clipboard-was-copied-from\/"},"modified":"2014-07-21T07:00:00","modified_gmt":"2014-07-21T07:00:00","slug":"how-can-i-get-the-url-to-the-web-page-the-clipboard-was-copied-from","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20140721-00\/?p=453","title":{"rendered":"How can I get the URL to the Web page the clipboard was copied from?"},"content":{"rendered":"<p>\nWhen you copy content from a Web page to the clipboard\nand then paste it into OneNote, OneNote pastes the content\nbut also annotates it\n&#8220;Pasted from &#8230;&#8221;.\nHow does OneNote know where the content was copied from?\n<\/p>\n<p>\nAs noted in\n<a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa767917%28v=VS.85%29.aspx\">\nthe documentation for the HTML clipboard format<\/a>,\nWeb browsers can provide an optional <code>Source&shy;URL<\/code>\nproperty to specify the Web page the HTML was copied from.\n<\/p>\n<p>\nLet&#8217;s write a Little Program that mimics what OneNote does,\nbut just in plain text, because I don&#8217;t want to try to parse HTML.\nThis is much easier to do in C#, because the BCL provides most\nof the helper functions.\n<\/p>\n<pre>\nusing System;\nusing System.IO;\nusing System.Windows;\nclass Program {\n [STAThread]\n public static void Main() {\n  System.Console.WriteLine(Clipboard.GetText());\n  using (var sr = new StringReader(\n               Clipboard.GetText(TextDataFormat.Html))) {\n   string s;\n   while ((s = sr.ReadLine()) != null) {\n    if (s.StartsWith(\"SourceURL:\")) {\n     System.Console.WriteLine(\"Copied from {0}\", s.Substring(10));\n     break;\n    }\n   }\n  }\n }\n}\n<\/pre>\n<p>\nFirst, we get the text from the clipboard and print it.\nThat&#8217;s the easy part.\n<\/p>\n<p>\nNext, we get the HTML text from the clipboard.\nThis is a bunch of text in a\n<a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa767917(v=VS.85).aspx\">\nparticular format<\/a>.\nWe look for an entry that specifies the\n<code>Source&shy;URL<\/code>;\nif we find it, then we print the URL.\n<\/p>\n<p>\nThis code is rather sloppy.\nFor example, if the HTML itself contains the string\n<code>SourceURL:haha-fakeout<\/code>, we risk misdetecting it\nas the source.\nTo do this properly, we would have to verify that the string\nappears in the header area of the HTML (before the first StartFragment).\n<\/p>\n<p>\nBut this is a Little Program, so I can skip all that stuff.\n<\/p>\n<p>\nHere&#8217;s a sketch of the equivalent C\/C++ version:\n<\/p>\n<pre>\nint __cdecl main(int, char **)\n{\n if (OpenClipboard(NULL)) {\n  \/\/ Obtain the Unicode text and print it\n  HANDLE h = GetClipboardData(CF_UNICODETEXT);\n  if (h) {\n   PCWSTR pszPlainText = GlobalLock(h);\n   ... print pszPlainText ...\n   GlobalUnlock(h);\n  }\n  \/\/ Obtain the HTML text and extract the SourceURL\n  h = GetClipboardData(RegisterClipboardFormat(TEXT(\"HTML Format\")));\n  if (h) {\n   PCSTR pszHtmlFormat = GlobalLock(h);\n   ... break pszHtmlFormat into lines ...\n   ... look for a line that begins with \"SourceURL:\" ...\n   ... if found, print it ...\n   GlobalUnlock(h);\n  }\n  CloseClipboard();\n }\n return 0;\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>When you copy content from a Web page to the clipboard and then paste it into OneNote, OneNote pastes the content but also annotates it &#8220;Pasted from &#8230;&#8221;. How does OneNote know where the content was copied from? As noted in the documentation for the HTML clipboard format, Web browsers can provide an optional Source&shy;URL [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-453","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>When you copy content from a Web page to the clipboard and then paste it into OneNote, OneNote pastes the content but also annotates it &#8220;Pasted from &#8230;&#8221;. How does OneNote know where the content was copied from? As noted in the documentation for the HTML clipboard format, Web browsers can provide an optional Source&shy;URL [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/453","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=453"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/453\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}