mirror of
https://github.com/chylex/TweetDuck.git
synced 2024-11-14 08:42:46 +01:00
51 lines
1.8 KiB
C#
51 lines
1.8 KiB
C#
using CefSharp;
|
|
using CefSharp.WinForms;
|
|
using System;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
|
|
namespace TweetDuck.Resources{
|
|
static class ScriptLoader{
|
|
private const string UrlPrefix = "td:";
|
|
|
|
public static string LoadResource(string name, bool silent = false){
|
|
try{
|
|
return File.ReadAllText(Path.Combine(Program.ScriptPath, name), Encoding.UTF8);
|
|
}catch(Exception ex){
|
|
if (!silent){
|
|
MessageBox.Show("Unfortunately, "+Program.BrandName+" could not load the "+name+" file. The program will continue running with limited functionality.\r\n\r\n"+ex.Message, Program.BrandName+" Has Failed :(", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static void ExecuteFile(ChromiumWebBrowser browser, string file){
|
|
ExecuteScript(browser, LoadResource(file), GetRootIdentifier(file));
|
|
}
|
|
|
|
public static void ExecuteFile(IFrame frame, string file){
|
|
ExecuteScript(frame, LoadResource(file), GetRootIdentifier(file));
|
|
}
|
|
|
|
public static void ExecuteScript(ChromiumWebBrowser browser, string script, string identifier){
|
|
if (script == null)return;
|
|
|
|
using(IFrame frame = browser.GetMainFrame()){
|
|
frame.ExecuteJavaScriptAsync(script, UrlPrefix+identifier, 1);
|
|
}
|
|
}
|
|
|
|
public static void ExecuteScript(IFrame frame, string script, string identifier){
|
|
if (script == null)return;
|
|
|
|
frame.ExecuteJavaScriptAsync(script, UrlPrefix+identifier, 1);
|
|
}
|
|
|
|
public static string GetRootIdentifier(string file){
|
|
return "root:"+Path.GetFileNameWithoutExtension(file);
|
|
}
|
|
}
|
|
}
|