1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-05-23 20:34:07 +02:00

Refactor ScriptLoader for easier script identifier handling

This commit is contained in:
chylex 2016-06-30 03:15:46 +02:00
parent 188369e922
commit b531016c7b
5 changed files with 33 additions and 12 deletions

View File

@ -122,11 +122,8 @@ private void Browser_LoadingStateChanged(object sender, LoadingStateChangedEvent
private void Browser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e){ private void Browser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e){
if (e.Frame.IsMain){ if (e.Frame.IsMain){
foreach(string js in ScriptLoader.LoadResources("code.js").Where(js => js != null)){ ScriptLoader.ExecuteFile(browser,"code.js","root:code");
browser.ExecuteScriptAsync(js); plugins_Reloaded(plugins,new PluginLoadEventArgs(new string[0]));
}
browser.ExecuteScriptAsync(plugins.GenerateScript(PluginEnvironment.Browser));
} }
} }
@ -198,7 +195,7 @@ private void plugins_Reloaded(object sender, PluginLoadEventArgs e){
} }
private void plugins_PluginChangedState(object sender, PluginChangedStateEventArgs e){ private void plugins_PluginChangedState(object sender, PluginChangedStateEventArgs e){
browser.ExecuteScriptAsync(PluginScriptGenerator.GenerateSetPluginState(e.Plugin,e.IsEnabled)); ScriptLoader.ExecuteScript(browser,PluginScriptGenerator.GenerateSetPluginState(e.Plugin,e.IsEnabled),"gen:pluginstate:"+e.Plugin);
} }
private void updates_UpdateAccepted(object sender, UpdateAcceptedEventArgs e){ private void updates_UpdateAccepted(object sender, UpdateAcceptedEventArgs e){

View File

@ -114,7 +114,7 @@ private void Config_MuteToggled(object sender, EventArgs e){
private void Browser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e){ private void Browser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e){
if (e.Frame.IsMain && notificationJS != null && browser.Address != "about:blank"){ if (e.Frame.IsMain && notificationJS != null && browser.Address != "about:blank"){
browser.ExecuteScriptAsync(notificationJS); ScriptLoader.ExecuteScript(e.Frame,notificationJS,"root:notification");
browser.ExecuteScriptAsync(plugins.GenerateScript(PluginEnvironment.Notification)); browser.ExecuteScriptAsync(plugins.GenerateScript(PluginEnvironment.Notification));
} }
} }

View File

@ -52,6 +52,10 @@ public string GetScriptPath(PluginEnvironment environment){
} }
} }
public override string ToString(){
return Identifier;
}
public override int GetHashCode(){ public override int GetHashCode(){
return identifier.GetHashCode(); return identifier.GetHashCode();
} }

View File

@ -4,9 +4,13 @@
using System.Text; using System.Text;
using System.Windows.Forms; using System.Windows.Forms;
using System.Linq; using System.Linq;
using CefSharp;
using CefSharp.WinForms;
namespace TweetDck.Resources{ namespace TweetDck.Resources{
static class ScriptLoader{ static class ScriptLoader{
private const string UrlPrefix = "td:";
public static string LoadResource(string name){ public static string LoadResource(string name){
try{ try{
return File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,name),Encoding.UTF8); return File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,name),Encoding.UTF8);
@ -16,8 +20,26 @@ public static string LoadResource(string name){
} }
} }
public static IEnumerable<string> LoadResources(params string[] names){ public static void ExecuteFile(ChromiumWebBrowser browser, string file, string identifier){
return names.Select(LoadResource); ExecuteScript(browser,LoadResource(file),identifier);
}
public static void ExecuteFile(IFrame frame, string file, string identifier){
ExecuteScript(frame,LoadResource(file),identifier);
}
public static void ExecuteScript(ChromiumWebBrowser browser, string script, string identifier){
if (script == null)return;
using(IFrame frame = browser.GetMainFrame()){
frame.ExecuteJavaScriptAsync(script,UrlPrefix+identifier);
}
}
public static void ExecuteScript(IFrame frame, string script, string identifier){
if (script == null)return;
frame.ExecuteJavaScriptAsync(script,UrlPrefix+identifier);
} }
} }
} }

View File

@ -25,9 +25,7 @@ public UpdateHandler(ChromiumWebBrowser browser, FormBrowser form){
private void browser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e){ private void browser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e){
if (e.Frame.IsMain){ if (e.Frame.IsMain){
foreach(string js in ScriptLoader.LoadResources("update.js").Where(js => js != null)){ ScriptLoader.ExecuteFile(e.Frame,"update.js","root:update");
browser.ExecuteScriptAsync(js);
}
} }
} }