2024-05-23 13:19:21 -04:00

213 lines
8.9 KiB
C#

using AutoLikerCefSharpWpf.Helper;
using CefSharp;
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Input;
namespace AutoLikerCefSharpWpf
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private const string DefaultUrlForAddedTabs = "https://www.instagram.com/";
public string AutoLikerVersion { get; set; }
private AutoLikerSettingsManager _alsm;
public MainWindow()
{
InitializeComponent();
// version
this.AutoLikerVersion = "v2.1.1-20240523";
this.DataContext = this;
// manage settings in UI
this._alsm = new AutoLikerSettingsManager(this);
// Mouse Events
Browser.MouseMove += (sender, args) =>
{
Point position = Mouse.GetPosition(Browser);
this.txtMouseX.Text = Convert.ToString(Math.Floor(position.X));
this.txtMouseY.Text = Convert.ToString(Math.Floor(position.Y));
};
Browser.JavascriptObjectRepository.ResolveObject += (sender, e) =>
{
var repo = e.ObjectRepository;
if (e.ObjectName == "boundAsync")
{
BindingOptions bindingOptions = null; //Binding options is an optional param, defaults to null
bindingOptions = BindingOptions.DefaultBinder; //Use the default binder to serialize values into complex objects
repo.Register("boundAsync", new JavaScriptBoundObject(this), isAsync: true, options: bindingOptions);
}
};
Browser.JavascriptObjectRepository.ObjectBoundInJavascript += (sender, e) =>
{
var name = e.ObjectName;
Debug.WriteLine($"Object {e.ObjectName} was bound successfully.");
};
//Wait for the page to finish loading (all resources will have been loaded, rendering is likely still happening)
Browser.LoadingStateChanged += (sender, args) =>
{
//Wait for the Page to finish loading
if (args.IsLoading == false)
{
//Browser.ExecuteScriptAsync("alert('All Resources Have Loaded');");
}
};
//Wait for the MainFrame to finish loading
Browser.FrameLoadEnd += (sender, args) =>
{
//Wait for the MainFrame to finish loading
if (args.Frame.IsMain)
{
if (args.Frame.Url.Contains("instagram.com/") && args.Frame.Url.Contains("#al"))
{
// Bind CEF object
string script = @"(async function()
{
await CefSharp.BindObjectAsync(""boundAsync"");
//The default is to camel case method names (the first letter of the method name is changed to lowercase)
console.log('boundAsync worked!');
})();";
args.Frame.ExecuteJavaScriptAsync(script);
// Inject JQuery
string jq = JavaScriptProcessor.ReturnJavaScriptFromFile("jquery-3.7.1.min.js");
args.Frame.ExecuteJavaScriptAsync(jq);
// Inject AutoLiker class
#if DEBUG
string aljs = JavaScriptProcessor.ReturnJavaScriptFromFile("autotag_injection.js");
#else
string aljs = JavaScriptProcessor.ReturnJavaScriptFromFile("autotag_injection_min.js");
#endif
args.Frame.ExecuteJavaScriptAsync(aljs);
this.Dispatcher.Invoke(() =>
{
// check which option to use
string paramTag = this.txtHashTag.Text;
if ((bool) this.rdTypeLocationTag.IsChecked)
{
paramTag = this.txtLocationTag.Text;
}
// Execute script
string alStartScript = $@"(function()
{{
if (typeof jQuery == 'undefined') {{
console.log('Sorry, but jQuery wasn\'t able to load');
}} else {{
jQuery.noConflict();
console.log('This page is now jQuerified with v' + jQuery.fn.jquery);
jQuery(document).ready(function () {{
requestAnimationFrame(() => {{
queueMicrotask(() => {{
console.log('finished painting');
al=new alObject();
al.initializeSettings('{this.txtHashTag.Text}',
{this.txtMaxLikesMin.Text},
{this.txtMaxLikesMax.Text},
{this.txtDelayMin.Text},
{this.txtDelayMax.Text},
{this.txtDelayRestartMin.Text},
{this.txtDelayRestartMax.Text});
al.instagramStartClickProcess();
}});
}});
}});
}}
}})();";
args.Frame.ExecuteJavaScriptAsync(alStartScript);
});
}
}
};
}
private void Button_DevTools(object sender, RoutedEventArgs e)
{
Browser.ShowDevTools();
}
private void BtnStart_Click(object sender, RoutedEventArgs e)
{
Debug.WriteLine("==== BtnStart_Click ====");
this.LoadInstragramTag();
}
private void BtnStop_Click(object sender, RoutedEventArgs e)
{
Debug.WriteLine("==== BtnStop_Click ====");
this.Dispatcher.Invoke(() =>
{
Debug.WriteLine("*** STOPPING ***");
this.LogMessage("Stopping Auto Liking");
// reload browser
this.Browser.LoadUrl(DefaultUrlForAddedTabs);
//this.txtLog.IsReadOnly = false;
});
}
private void BtnSaveSettings_Click(object sender, RoutedEventArgs e)
{
Debug.WriteLine("==== BtnSaveSettings_Click ====");
this._alsm.AutoTagSettingsSave();
}
public void LoadInstragramTag()
{
Debug.WriteLine("==== LoadInstragramTag ====");
this.Dispatcher.Invoke(() =>
{
this.txtLog.Clear();
string URL = "https://www.instagram.com/explore/tags/";
string hashtag = this.txtHashTag.Text;
if ((bool) this.rdTypeLocationTag.IsChecked)
{
URL = "https://www.instagram.com/explore/locations/";
hashtag = this.txtLocationTag.Text;
}
// check if multiple hastags
if (hashtag.Contains(","))
{
string[] hashtags = hashtag.Split(',');
int hashtags_length = hashtags.Length;
Debug.WriteLine(" - hashtags_length = " + hashtags_length);
Random rnd = new Random();
int hashtag_index = rnd.Next(hashtags_length);
Debug.WriteLine(" - hashtag_index = " + hashtag_index);
hashtag = hashtags.GetValue(hashtag_index).ToString().Trim();
}
this.LogMessage("Load Instagram Tag: " + hashtag);
// create URL
URL += hashtag + "/#al";
// load URL
this.Browser.Load(URL);
});
}
/// <summary>
/// Log message in UI text box
/// </summary>
/// <param name="msg">String message</param>
public void LogMessage(string msg)
{
this.Dispatcher.Invoke(() =>
{
Debug.WriteLine(msg);
this.txtLog.AppendText(msg + Environment.NewLine);
this.txtLog.ScrollToEnd();
});
}
}
}