diff --git a/Core/TweetDeckBrowser.cs b/Core/TweetDeckBrowser.cs index 67af5243..26c42b54 100644 --- a/Core/TweetDeckBrowser.cs +++ b/Core/TweetDeckBrowser.cs @@ -131,7 +131,7 @@ private void browser_FrameLoadStart(object sender, FrameLoadStartEventArgs e){ } if (TwitterUtils.IsTwitterWebsite(frame)){ - ScriptLoader.ExecuteFile(frame, "twitter.js"); + ScriptLoader.ExecuteFile(frame, "twitter.js", browser); } frame.ExecuteJavaScriptAsync(TwitterUtils.BackgroundColorOverride); @@ -144,7 +144,7 @@ private void browser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e){ if (frame.IsMain && TwitterUtils.IsTweetDeckWebsite(frame)){ UpdateProperties(); TweetDeckBridge.RestoreSessionData(frame); - ScriptLoader.ExecuteFile(frame, "code.js"); + ScriptLoader.ExecuteFile(frame, "code.js", browser); InjectBrowserCSS(); ReinjectCustomCSS(Program.UserConfig.CustomBrowserCSS); UserConfig_SoundNotificationInfoChanged(null, EventArgs.Empty); @@ -152,7 +152,7 @@ private void browser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e){ TweetDeckBridge.ResetStaticProperties(); if (Program.UserConfig.FirstRun){ - ScriptLoader.ExecuteFile(frame, "introduction.js"); + ScriptLoader.ExecuteFile(frame, "introduction.js", browser); } } } @@ -212,7 +212,7 @@ public void UpdateProperties(){ } public void InjectBrowserCSS(){ - browser.ExecuteScriptAsync("TDGF_injectBrowserCSS", ScriptLoader.LoadResource("styles/browser.css")?.TrimEnd() ?? string.Empty); + browser.ExecuteScriptAsync("TDGF_injectBrowserCSS", ScriptLoader.LoadResource("styles/browser.css", false, browser)?.TrimEnd() ?? string.Empty); } public void ReinjectCustomCSS(string css){ diff --git a/Resources/ScriptLoader.cs b/Resources/ScriptLoader.cs index 5954109c..230699f0 100644 --- a/Resources/ScriptLoader.cs +++ b/Resources/ScriptLoader.cs @@ -2,26 +2,30 @@ using System; using System.IO; using System.Text; +using System.Windows.Forms; +using TweetDuck.Core.Controls; using TweetDuck.Core.Other; namespace TweetDuck.Resources{ static class ScriptLoader{ private const string UrlPrefix = "td:"; - public static string LoadResource(string name, bool silent = false){ + public static string LoadResource(string name, bool silent = false, Control sync = null){ try{ return File.ReadAllText(Path.Combine(Program.ScriptPath, name), Encoding.UTF8); }catch(Exception ex){ if (!silent){ - FormMessage.Error("TweetDuck Has Failed :(", "Unfortunately, TweetDuck could not load the "+name+" file. The program will continue running with limited functionality.\n\n"+ex.Message, FormMessage.OK); + ShowLoadError(sync, "Unfortunately, TweetDuck could not load the "+name+" file. The program will continue running with limited functionality.\n\n"+ex.Message); } return null; } } - public static void ExecuteFile(IFrame frame, string file){ - ExecuteScript(frame, LoadResource(file), GetRootIdentifier(file)); + public static bool ExecuteFile(IFrame frame, string file, Control sync = null){ + string script = LoadResource(file, sync == null, sync); + ExecuteScript(frame, script, GetRootIdentifier(file)); + return script != null; } public static void ExecuteScript(IFrame frame, string script, string identifier){ @@ -33,5 +37,14 @@ public static void ExecuteScript(IFrame frame, string script, string identifier) public static string GetRootIdentifier(string file){ return "root:"+Path.GetFileNameWithoutExtension(file); } + + private static void ShowLoadError(Control sync, string message){ + if (sync == null){ + FormMessage.Error("TweetDuck Has Failed :(", message, FormMessage.OK); + } + else{ + sync.InvokeAsyncSafe(() => FormMessage.Error("TweetDuck Has Failed :(", message, FormMessage.OK)); + } + } } } diff --git a/Updates/UpdateHandler.cs b/Updates/UpdateHandler.cs index 94574f57..0bbdbc3f 100644 --- a/Updates/UpdateHandler.cs +++ b/Updates/UpdateHandler.cs @@ -30,7 +30,7 @@ public UpdateHandler(ITweetDeckBrowser browser, UpdaterSettings settings){ } private void OnFrameLoaded(IFrame frame){ - ScriptLoader.ExecuteFile(frame, "update.js"); + ScriptLoader.ExecuteFile(frame, "update.js"); // TODO can't show error on failure } public int Check(bool force){