Skip to content

Commit

Permalink
feat: 🎉 first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
iNViTiON committed Jul 17, 2022
0 parents commit f44275a
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.vs
/bin
/obj
11 changes: 11 additions & 0 deletions ClipboardTextReplacer.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>

</Project>
25 changes: 25 additions & 0 deletions ClipboardTextReplacer.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.1.32210.238
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ClipboardTextReplacer", "ClipboardTextReplacer.csproj", "{BB2D237B-323A-4C59-BB21-D99F3CBF4B94}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{BB2D237B-323A-4C59-BB21-D99F3CBF4B94}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BB2D237B-323A-4C59-BB21-D99F3CBF4B94}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BB2D237B-323A-4C59-BB21-D99F3CBF4B94}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BB2D237B-323A-4C59-BB21-D99F3CBF4B94}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {49917942-87E0-4584-ADCE-235B02B4C04B}
EndGlobalSection
EndGlobal
151 changes: 151 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
// Clipboard text replacer for automate clipboard text replace
// Onion site to normal site & Query string remover
// iNViTiON
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using System.Text;

// https://stackoverflow.com/questions/621577/clipboard-event-c-sharp
// https://stackoverflow.com/questions/17762037/error-while-trying-to-copy-string-to-clipboard
// https://gist.github.com/glombard/7986317
// https://stackoverflow.com/questions/38148400/clipboard-monitor

internal static class NativeMethods
{
// Reference https://docs.microsoft.com/en-us/windows/desktop/dataxchg/wm-clipboardupdate
public const int WM_CLIPBOARDUPDATE = 0x031D;
// Reference https://www.pinvoke.net/default.aspx/Constants.HWND
public static IntPtr HWND_MESSAGE = new IntPtr(-3);

// Reference https://www.pinvoke.net/default.aspx/user32/AddClipboardFormatListener.html
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool AddClipboardFormatListener(IntPtr hwnd);

// Reference https://www.pinvoke.net/default.aspx/user32/RemoveClipboardFormatListener.html
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool RemoveClipboardFormatListener(IntPtr hwnd);

// Reference https://www.pinvoke.net/default.aspx/user32.setparent
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

// Reference https://www.pinvoke.net/default.aspx/user32/getwindowtext.html
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

// Reference https://www.pinvoke.net/default.aspx/user32.getwindowtextlength
[DllImport("user32.dll")]
public static extern int GetWindowTextLength(IntPtr hWnd);

// Reference https://www.pinvoke.net/default.aspx/user32.getforegroundwindow
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
}

public static class Clipboard
{
public static string GetText()
{
string ReturnValue = string.Empty;
Thread STAThread = new Thread(
delegate ()
{
// Use a fully qualified name for Clipboard otherwise it
// will end up calling itself.
ReturnValue = System.Windows.Forms.Clipboard.GetText();
});
STAThread.SetApartmentState(ApartmentState.STA);
STAThread.Start();
STAThread.Join();

return ReturnValue;
}

public static bool IsText()
{
bool ReturnValue = false;
Thread STAThread = new Thread(
delegate ()
{
ReturnValue = System.Windows.Forms.Clipboard.ContainsText();
});
STAThread.SetApartmentState(ApartmentState.STA);
STAThread.Start();
STAThread.Join();

return ReturnValue;
}
}

public sealed class ClipboardNotification
{
private class NotificationForm : Form
{
// normal text replace
(string from, string to)[] pairs = {
("bbcweb3hytmzhn5d532owbu6oqadra5z3ar726vq5kgwwn6aucdccrad.onion", "bbc.com"),
("duckduckgogg42xjoc72x3sjasowoarfbgcmvfimaftt6twagswzczad.onion", "duckduckgo.com"),
("facebookwkhpilnemxj7asaniu7vnjjbiltxjqhye3mhbshg7kx5tfyd.onion", "facebook.com"),
("twitter3e4tixl4xyajtrzo62zg5vztmjuricljdp2c5kshju4avyoid.onion", "twitter.com"),
};
// Regex text replace
(string regex, string to)[] regexPairs = {
/*
* Facebook
* __cft__[0]
* __tn__
* hoisted_section_header_type
* sfnsn
* Twitter
* t
* s
* Tiktok
* k
*/
("&?(__cft__\\[0\\]|__tn__|hoisted_section_header_type|sfnsn|t|s|k)(=.+?)(&|$)", ""),
// remove "?" if empty query param
("\\?$", ""),
};

public NotificationForm()
{
// Turn the child window into a message-only window (refer to Microsoft docs)
NativeMethods.SetParent(Handle, NativeMethods.HWND_MESSAGE);
// Place window in the system-maintained clipboard format listener list
NativeMethods.AddClipboardFormatListener(Handle);
}

private void SetClipboard(string text)
{
// Deactivate listener to prevent infinite loop
NativeMethods.RemoveClipboardFormatListener(Handle);
System.Windows.Forms.Clipboard.SetText(text);
NativeMethods.AddClipboardFormatListener(Handle);
}

protected override void WndProc(ref Message m)
{
// Listen for operating system messages
if (m.Msg == NativeMethods.WM_CLIPBOARDUPDATE && Clipboard.IsText())
{
string content = Clipboard.GetText();
string replace = content;
for (int i = 0; i < pairs.GetLength(0); ++i) replace = replace.Replace(pairs[i].from, pairs[i].to);
for (int i = 0; i < regexPairs.GetLength(0); ++i) replace = Regex.Replace(replace, regexPairs[i].regex, regexPairs[i].to);
if (content != replace) SetClipboard(replace);
}
// Called for any unhandled messages
base.WndProc(ref m);
}
}

[STAThread]
private static void Main(string[] args)
{
// starts a message loop on current thread and displays specified form
Application.Run(new NotificationForm());
}
}
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# ClipboardTextReplacer

Automatic replace text in clipboard with another text for Windows.

e.g. copy `https://www.facebookwkhpilnemxj7asaniu7vnjjbiltxjqhye3mhbshg7kx5tfyd.onion` and get `https://www.facebook.com` in clipboard.

Advance regex text replace can use to strip tracking query param from url.

- Facebook
- `__cft__[0]`
- `__tn__`
- `hoisted_section_header_type`
- `sfnsn`
- Twitter
- `t`
- `s`
- Tiktok
- `k`

### Example

```
https://www.facebook.com/facebookcorewwwi/posts/pfbid0ekRfSNJAynHTQkgHd7F3n9CdutckYt6jomBf6r9Kfj68Q7nZ1WeLDAks57AZjeEpl?__cft__[0]=AZW2tok2MJV_-COmmtzi2y_9frEXRGg4Uamt6nhDhsiNelMriyV_8eRevDN_BVFRkmz6H1omjqd0450s-sGmXbT_XrR28mG_oyITa4rD-SY1b8iujzJsI7c-ZTnI0M0RLNeJxSAv49-z8kPcOwv0zz_LZvIUMjYcx-c4zfjPWydKJeavwTetD2vDrXh-sQbrN4M&__tn__=%2CO%2CP-R&type=3&hoisted_section_header_type=header&sfnsn=1
```
||
V
```
https://www.facebook.com/facebookcorewwwi/posts/pfbid0ekRfSNJAynHTQkgHd7F3n9CdutckYt6jomBf6r9Kfj68Q7nZ1WeLDAks57AZjeEpl?type=3
```
and

```
https://twitter.com/Twitter/status/1509206476874784769?t=xnSc6C0qmYaIu6BVsQeJ-w&s=19
```

||
V
```
https://twitter.com/Twitter/status/1509206476874784769
```

0 comments on commit f44275a

Please sign in to comment.