1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-05-04 08: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){
if (e.Frame.IsMain){
foreach(string js in ScriptLoader.LoadResources("code.js").Where(js => js != null)){
browser.ExecuteScriptAsync(js);
}
browser.ExecuteScriptAsync(plugins.GenerateScript(PluginEnvironment.Browser));
ScriptLoader.ExecuteFile(browser,"code.js","root:code");
plugins_Reloaded(plugins,new PluginLoadEventArgs(new string[0]));
}
}
@ -198,7 +195,7 @@ private void plugins_Reloaded(object sender, PluginLoadEventArgs 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){

View File

@ -114,7 +114,7 @@ private void Config_MuteToggled(object sender, EventArgs e){
private void Browser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e){
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));
}
}

View File

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

View File

@ -4,9 +4,13 @@
using System.Text;
using System.Windows.Forms;
using System.Linq;
using CefSharp;
using CefSharp.WinForms;
namespace TweetDck.Resources{
static class ScriptLoader{
private const string UrlPrefix = "td:";
public static string LoadResource(string name){
try{
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){
return names.Select(LoadResource);
public static void ExecuteFile(ChromiumWebBrowser browser, string file, string identifier){
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){
if (e.Frame.IsMain){
foreach(string js in ScriptLoader.LoadResources("update.js").Where(js => js != null)){
browser.ExecuteScriptAsync(js);
}
ScriptLoader.ExecuteFile(e.Frame,"update.js","root:update");
}
}