diff --git a/Core/Bridge/TweetDeckBridge.cs b/Core/Bridge/TweetDeckBridge.cs index 25e49809..5e42115f 100644 --- a/Core/Bridge/TweetDeckBridge.cs +++ b/Core/Bridge/TweetDeckBridge.cs @@ -166,6 +166,10 @@ public void ScreenshotTweet(string html, int width, int height){ form.InvokeSafe(() => form.OnTweetScreenshotReady(html, width, height)); } + public void FixClipboard(){ + form.InvokeSafe(WindowsUtils.ClipboardStripHtmlStyles); + } + public void OpenBrowser(string url){ BrowserUtils.OpenExternalBrowser(url); } diff --git a/Core/Utils/WindowsUtils.cs b/Core/Utils/WindowsUtils.cs index 946b9937..af009a18 100644 --- a/Core/Utils/WindowsUtils.cs +++ b/Core/Utils/WindowsUtils.cs @@ -1,9 +1,13 @@ using System.Diagnostics; using System.IO; +using System.Text.RegularExpressions; using System.Windows.Forms; namespace TweetDck.Core.Utils{ static class WindowsUtils{ + private static readonly Regex RegexStripHtmlStyles = new Regex(@"\s?(?:style|class)="".*?"""); + private static readonly Regex RegexOffsetClipboardHtml = new Regex(@"(?<=EndHTML:|EndFragment:)(\d+)"); + public static bool CheckFolderWritePermission(string path){ string testFile = Path.Combine(path, ".test"); @@ -39,5 +43,19 @@ public static Timer CreateSingleTickTimer(int timeout){ timer.Tick += (sender, args) => timer.Stop(); return timer; } + + public static void ClipboardStripHtmlStyles(){ + if (!Clipboard.ContainsText(TextDataFormat.Html)){ + return; + } + + string original = Clipboard.GetText(TextDataFormat.Html); + string updated = RegexStripHtmlStyles.Replace(original, string.Empty); + + int removed = original.Length-updated.Length; + updated = RegexOffsetClipboardHtml.Replace(updated, match => (int.Parse(match.Value)-removed).ToString().PadLeft(match.Value.Length, '0')); + + Clipboard.SetText(updated, TextDataFormat.Html); + } } } diff --git a/Resources/Scripts/code.js b/Resources/Scripts/code.js index 1cd913ec..97a66907 100644 --- a/Resources/Scripts/code.js +++ b/Resources/Scripts/code.js @@ -524,6 +524,13 @@ }); })(); + // + // Block: Work around clipboard HTML formatting. + // + $(document).on("copy", function(e){ + window.setTimeout($TD.fixClipboard, 0); + }); + // // Block: Inject custom CSS and layout into the page. //