mirror of
https://github.com/chylex/TweetDuck.git
synced 2025-05-02 02:34:08 +02:00
56 lines
2.2 KiB
C#
56 lines
2.2 KiB
C#
using System;
|
|
using System.Drawing;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text.RegularExpressions;
|
|
using System.Windows.Forms;
|
|
|
|
namespace TweetDuck.Management{
|
|
static class ClipboardManager{
|
|
private static readonly Lazy<Regex> RegexStripHtmlStyles = new Lazy<Regex>(() => new Regex(@"\s?(?:style|class)="".*?"""), false);
|
|
private static readonly Lazy<Regex> RegexOffsetClipboardHtml = new Lazy<Regex>(() => new Regex(@"(?<=EndHTML:|EndFragment:)(\d+)"), false);
|
|
|
|
public static void SetText(string text, TextDataFormat format){
|
|
if (string.IsNullOrEmpty(text)){
|
|
return;
|
|
}
|
|
|
|
DataObject obj = new DataObject();
|
|
obj.SetText(text, format);
|
|
SetClipboardData(obj);
|
|
}
|
|
|
|
public static void SetImage(Image image){
|
|
DataObject obj = new DataObject();
|
|
obj.SetImage(image);
|
|
SetClipboardData(obj);
|
|
}
|
|
|
|
private static void SetClipboardData(DataObject obj){
|
|
try{
|
|
Clipboard.SetDataObject(obj);
|
|
}catch(ExternalException e){
|
|
Program.Reporter.HandleException("Clipboard Error", "TweetDuck could not access the clipboard as it is currently used by another process.", true, e);
|
|
}
|
|
}
|
|
|
|
public static void StripHtmlStyles(){
|
|
if (!Clipboard.ContainsText(TextDataFormat.Html) || !Clipboard.ContainsText(TextDataFormat.UnicodeText)){
|
|
return;
|
|
}
|
|
|
|
string originalText = Clipboard.GetText(TextDataFormat.UnicodeText);
|
|
string originalHtml = Clipboard.GetText(TextDataFormat.Html);
|
|
|
|
string updatedHtml = RegexStripHtmlStyles.Value.Replace(originalHtml, string.Empty);
|
|
|
|
int removed = originalHtml.Length - updatedHtml.Length;
|
|
updatedHtml = RegexOffsetClipboardHtml.Value.Replace(updatedHtml, match => (int.Parse(match.Value) - removed).ToString().PadLeft(match.Value.Length, '0'));
|
|
|
|
DataObject obj = new DataObject();
|
|
obj.SetText(originalText, TextDataFormat.UnicodeText);
|
|
obj.SetText(updatedHtml, TextDataFormat.Html);
|
|
SetClipboardData(obj);
|
|
}
|
|
}
|
|
}
|