1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-04-09 06:15:49 +02:00

Add javascript injection with a bridge object to the website

This commit is contained in:
chylex 2016-04-10 17:21:11 +02:00
parent 6c53f10810
commit d7a0f577d6
4 changed files with 62 additions and 0 deletions

View File

@ -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);
}
}

14
Core/TweetDeckBridge.cs Normal file
View File

@ -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);
}
}
}

31
Resources/code.js Normal file
View File

@ -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);

View File

@ -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">