diff --git a/Core/FormBrowser.cs b/Core/FormBrowser.cs index 7ec7de1c..48c68ac0 100644 --- a/Core/FormBrowser.cs +++ b/Core/FormBrowser.cs @@ -4,6 +4,8 @@ using System.Linq; using TweetDick.Configuration; using CefSharp; +using System.IO; +using System.Text; namespace TweetDick.Core{ public partial class FormBrowser : Form{ @@ -14,12 +16,17 @@ private static UserConfig Config{ } private readonly ChromiumWebBrowser browser; + private readonly TweetDeckBridge bridge; public FormBrowser(){ InitializeComponent(); + bridge = new TweetDeckBridge(this); + browser = new ChromiumWebBrowser("https://tweetdeck.twitter.com/"); browser.LoadingStateChanged += Browser_LoadingStateChanged; + browser.RegisterJsObject("$TD",bridge); + Controls.Add(browser); } @@ -54,6 +61,9 @@ private void Browser_LoadingStateChanged(object sender, LoadingStateChangedEvent if (!e.IsLoading){ Invoke(new Action(SetupWindow)); browser.LoadingStateChanged -= Browser_LoadingStateChanged; + + string js = File.ReadAllText("code.js",Encoding.UTF8); + browser.ExecuteScriptAsync(js); } } diff --git a/Core/TweetDeckBridge.cs b/Core/TweetDeckBridge.cs new file mode 100644 index 00000000..1339ae25 --- /dev/null +++ b/Core/TweetDeckBridge.cs @@ -0,0 +1,14 @@ +using System.Windows.Forms; +namespace TweetDick.Core{ + class TweetDeckBridge{ + private readonly FormBrowser form; + + public TweetDeckBridge(FormBrowser form){ + this.form = form; + } + + public void Log(string data){ + System.Diagnostics.Debug.WriteLine(data); + } + } +} diff --git a/Resources/code.js b/Resources/code.js new file mode 100644 index 00000000..59a9fb56 --- /dev/null +++ b/Resources/code.js @@ -0,0 +1,31 @@ +(function($,$TD){ + // + // Variable: Says whether TweetDick events was initialized. + // + var isInitialized = false; + + // + // Function: Initializes TweetDick events. Called after the website app is loaded. + // + var initializeTweetDick = function(){ + isInitialized = true; + }; + + // + // Block: Observe the app <div> element and initialize TweetDick whenever possible. + // + var app = $("body").children(".js-app"); + + new MutationObserver(function(mutations){ + if (mutations.some(mutation => mutation.attributeName === "class")){ + if (isInitialized && app.hasClass("is-hidden")){ + isInitialized = false; + } + else if (!isInitialized && !app.hasClass("is-hidden")){ + initializeTweetDick(); + } + } + }).observe(app[0],{ + attributes: true + }); +})($,$TD); diff --git a/TweetDick.csproj b/TweetDick.csproj index 8976deff..ea261d7a 100644 --- a/TweetDick.csproj +++ b/TweetDick.csproj @@ -97,6 +97,7 @@ <Compile Include="Core\FormBackgroundWork.Designer.cs"> <DependentUpon>FormBackgroundWork.cs</DependentUpon> </Compile> + <Compile Include="Core\TweetDeckBridge.cs" /> <Compile Include="Migration\FormMigrationQuestion.cs"> <SubType>Form</SubType> </Compile> @@ -146,6 +147,12 @@ <EmbedInteropTypes>True</EmbedInteropTypes> </COMReference> </ItemGroup> + <ItemGroup> + <ContentWithTargetPath Include="Resources\code.js"> + <CopyToOutputDirectory>Always</CopyToOutputDirectory> + <TargetPath>code.js</TargetPath> + </ContentWithTargetPath> + </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="packages\cef.redist.x86.3.2526.1362\build\cef.redist.x86.targets" Condition="Exists('packages\cef.redist.x86.3.2526.1362\build\cef.redist.x86.targets')" /> <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">