mirror of
https://github.com/chylex/TweetDuck.git
synced 2025-05-25 08:34:05 +02:00
Rewrite plugins.js, plugin state handling and script execution (separate instead of combining)
This commit is contained in:
parent
dc78c68f12
commit
b6a683dfe1
Core
Plugins
Resources/Scripts
@ -122,8 +122,9 @@ 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){
|
||||||
ScriptLoader.ExecuteFile(browser,"code.js");
|
ScriptLoader.ExecuteFile(e.Frame,"code.js");
|
||||||
plugins_Reloaded(plugins,new PluginLoadEventArgs(new string[0]));
|
ScriptLoader.ExecuteFile(e.Frame,PluginManager.PluginScriptFile);
|
||||||
|
plugins.ExecutePlugins(e.Frame,PluginEnvironment.Browser);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -191,11 +192,11 @@ private void trayIcon_ClickClose(object sender, EventArgs e){
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void plugins_Reloaded(object sender, PluginLoadEventArgs e){
|
private void plugins_Reloaded(object sender, PluginLoadEventArgs e){
|
||||||
browser.ExecuteScriptAsync(plugins.GenerateScript(PluginEnvironment.Browser));
|
browser.ExecuteScriptAsync("window.location.reload()");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void plugins_PluginChangedState(object sender, PluginChangedStateEventArgs e){
|
private void plugins_PluginChangedState(object sender, PluginChangedStateEventArgs e){
|
||||||
ScriptLoader.ExecuteScript(browser,PluginScriptGenerator.GenerateSetPluginState(e.Plugin,e.IsEnabled),"gen:pluginstate:"+e.Plugin);
|
browser.ExecuteScriptAsync("window.TDPF_setPluginState",e.Plugin,e.IsEnabled ? 1 : 0); // ExecuteScriptAsync cannot handle boolean values as of yet
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updates_UpdateAccepted(object sender, UpdateAcceptedEventArgs e){
|
private void updates_UpdateAccepted(object sender, UpdateAcceptedEventArgs e){
|
||||||
|
@ -115,7 +115,8 @@ 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"){
|
||||||
ScriptLoader.ExecuteScript(e.Frame,notificationJS,"root:notification");
|
ScriptLoader.ExecuteScript(e.Frame,notificationJS,"root:notification");
|
||||||
browser.ExecuteScriptAsync(plugins.GenerateScript(PluginEnvironment.Notification));
|
ScriptLoader.ExecuteFile(e.Frame,PluginManager.PluginScriptFile);
|
||||||
|
plugins.ExecutePlugins(e.Frame,PluginEnvironment.Notification);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,12 +2,14 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using CefSharp;
|
||||||
using TweetDck.Plugins.Events;
|
using TweetDck.Plugins.Events;
|
||||||
using TweetDck.Resources;
|
using TweetDck.Resources;
|
||||||
|
|
||||||
namespace TweetDck.Plugins{
|
namespace TweetDck.Plugins{
|
||||||
class PluginManager{
|
class PluginManager{
|
||||||
|
public const string PluginScriptFile = "plugins.js";
|
||||||
|
|
||||||
public string PathOfficialPlugins { get { return Path.Combine(rootPath,"official"); } }
|
public string PathOfficialPlugins { get { return Path.Combine(rootPath,"official"); } }
|
||||||
public string PathCustomPlugins { get { return Path.Combine(rootPath,"user"); } }
|
public string PathCustomPlugins { get { return Path.Combine(rootPath,"user"); } }
|
||||||
|
|
||||||
@ -53,29 +55,24 @@ public void Reload(){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GenerateScript(PluginEnvironment environment){
|
public void ExecutePlugins(IFrame frame, PluginEnvironment environment){
|
||||||
string mainScript = ScriptLoader.LoadResource("plugins.js");
|
ScriptLoader.ExecuteScript(frame,PluginScriptGenerator.GenerateConfig(Config),"gen:pluginconfig");
|
||||||
if (mainScript == null)return string.Empty;
|
|
||||||
|
|
||||||
StringBuilder build = new StringBuilder((1+plugins.Count)*512);
|
|
||||||
|
|
||||||
PluginScriptGenerator.AppendStart(build,environment);
|
|
||||||
build.Append(mainScript);
|
|
||||||
PluginScriptGenerator.AppendConfig(build,Config);
|
|
||||||
|
|
||||||
foreach(Plugin plugin in Plugins){
|
foreach(Plugin plugin in Plugins){
|
||||||
string path = plugin.GetScriptPath(environment);
|
string path = plugin.GetScriptPath(environment);
|
||||||
if (string.IsNullOrEmpty(path) || !plugin.CanRun)continue;
|
if (string.IsNullOrEmpty(path) || !plugin.CanRun)continue;
|
||||||
|
|
||||||
|
string script;
|
||||||
|
|
||||||
try{
|
try{
|
||||||
PluginScriptGenerator.AppendPlugin(build,plugin.Identifier,File.ReadAllText(path));
|
script = File.ReadAllText(path);
|
||||||
}catch{
|
}catch{
|
||||||
// TODO
|
// TODO
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ScriptLoader.ExecuteScript(frame,PluginScriptGenerator.GeneratePlugin(plugin.Identifier,script,environment),"plugin:"+plugin);
|
||||||
}
|
}
|
||||||
|
|
||||||
PluginScriptGenerator.AppendEnd(build,environment);
|
|
||||||
return build.ToString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private IEnumerable<Plugin> LoadPluginsFrom(string path, PluginGroup group){
|
private IEnumerable<Plugin> LoadPluginsFrom(string path, PluginGroup group){
|
||||||
|
@ -2,30 +2,23 @@
|
|||||||
|
|
||||||
namespace TweetDck.Plugins{
|
namespace TweetDck.Plugins{
|
||||||
static class PluginScriptGenerator{
|
static class PluginScriptGenerator{
|
||||||
public static void AppendStart(StringBuilder build, PluginEnvironment environment){
|
public static string GenerateConfig(PluginConfig config){
|
||||||
|
return config.AnyDisabled ? "window.TD_PLUGINS.disabled = [\""+string.Join("\",\"",config.DisabledPlugins)+"\"];" : string.Empty;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string GeneratePlugin(string pluginIdentifier, string pluginContents, PluginEnvironment environment){
|
||||||
|
StringBuilder build = new StringBuilder(pluginIdentifier.Length+pluginContents.Length+110);
|
||||||
|
|
||||||
build.Append("(function(").Append(environment.GetScriptVariables()).Append("){");
|
build.Append("(function(").Append(environment.GetScriptVariables()).Append("){");
|
||||||
}
|
|
||||||
|
build.Append("window.TD_PLUGINS.install({");
|
||||||
public static void AppendConfig(StringBuilder build, PluginConfig config){
|
build.Append("id:\"").Append(pluginIdentifier).Append("\",");
|
||||||
if (config.AnyDisabled){
|
build.Append("obj:new class extends PluginBase{").Append(pluginContents).Append("}");
|
||||||
build.Append("PLUGINS.disabled = [\"").Append(string.Join("\",\"",config.DisabledPlugins)).Append("\"];");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void AppendPlugin(StringBuilder build, string pluginIdentifier, string pluginContents){
|
|
||||||
build.Append("PLUGINS.installed.push({");
|
|
||||||
build.Append("id: \"").Append(pluginIdentifier).Append("\",");
|
|
||||||
build.Append("obj: new class extends PluginBase{ ").Append(pluginContents).Append(" }");
|
|
||||||
build.Append("});");
|
build.Append("});");
|
||||||
}
|
|
||||||
|
|
||||||
public static void AppendEnd(StringBuilder build, PluginEnvironment environment){
|
|
||||||
build.Append("PLUGINS.load();");
|
|
||||||
build.Append("})(").Append(environment.GetScriptVariables()).Append(");");
|
build.Append("})(").Append(environment.GetScriptVariables()).Append(");");
|
||||||
}
|
|
||||||
|
|
||||||
public static string GenerateSetPluginState(Plugin plugin, bool enabled){
|
return build.ToString();
|
||||||
return new StringBuilder().Append("window.TD_PLUGINS.setState(\"").Append(plugin.Identifier).Append("\",").Append(enabled ? "true" : "false").Append(");").ToString();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,76 +1,80 @@
|
|||||||
var isReloading = "TD_PLUGINS" in window;
|
(function(){
|
||||||
|
//
|
||||||
if (isReloading){
|
// Class: Abstract plugin base class.
|
||||||
window.TD_PLUGINS.installed.forEach(plugin => {
|
//
|
||||||
if (!window.TD_PLUGINS.isDisabled(plugin)){
|
window.PluginBase = class{
|
||||||
plugin.obj.disabled();
|
constructor(pluginSettings){
|
||||||
|
this.$pluginSettings = pluginSettings || {};
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
class PluginBase{
|
enabled(){}
|
||||||
constructor(pluginSettings){
|
ready(){}
|
||||||
this.$pluginSettings = pluginSettings || {};
|
disabled(){}
|
||||||
}
|
};
|
||||||
|
|
||||||
enabled(){}
|
//
|
||||||
ready(){}
|
// Variable: Main object for containing and managing plugins.
|
||||||
disabled(){}
|
//
|
||||||
}
|
window.TD_PLUGINS = new class{
|
||||||
|
constructor(){
|
||||||
var PLUGINS = {
|
this.installed = [];
|
||||||
installed: [],
|
this.disabled = [];
|
||||||
disabled: [],
|
this.waiting = [];
|
||||||
waiting: [],
|
}
|
||||||
|
|
||||||
isDisabled: plugin => PLUGINS.disabled.includes(plugin.id),
|
isDisabled(plugin){
|
||||||
findObject: identifier => PLUGINS.installed.find(plugin => plugin.id === identifier),
|
return this.disabled.includes(plugin.id);
|
||||||
|
}
|
||||||
load: function(){
|
|
||||||
PLUGINS.installed.forEach(plugin => {
|
findObject(identifier){
|
||||||
if (!PLUGINS.isDisabled(plugin)){
|
return this.installed.find(plugin => plugin.id === identifier);
|
||||||
|
}
|
||||||
|
|
||||||
|
install(plugin){
|
||||||
|
this.installed.push(plugin);
|
||||||
|
|
||||||
|
if (!this.isDisabled(plugin)){
|
||||||
plugin.obj.enabled();
|
plugin.obj.enabled();
|
||||||
PLUGINS.runWhenReady(plugin);
|
this.runWhenReady(plugin);
|
||||||
}
|
}
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
onReady: function(){
|
|
||||||
PLUGINS.waiting.forEach(plugin => plugin.obj.ready());
|
|
||||||
PLUGINS.waiting = [];
|
|
||||||
},
|
|
||||||
|
|
||||||
runWhenReady: function(plugin){
|
|
||||||
if (window.TD_APP_READY){
|
|
||||||
plugin.obj.ready();
|
|
||||||
}
|
}
|
||||||
else{
|
|
||||||
PLUGINS.waiting.push(plugin);
|
runWhenReady(plugin){
|
||||||
|
if (window.TD_APP_READY){
|
||||||
|
plugin.obj.ready();
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
this.waiting.push(plugin);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
|
||||||
|
setState(plugin, enable){
|
||||||
|
if (enable && this.isDisabled(plugin)){
|
||||||
|
this.disabled.splice(this.disabled.indexOf(plugin.id),1);
|
||||||
|
plugin.obj.enabled();
|
||||||
|
this.runWhenReady(plugin);
|
||||||
|
}
|
||||||
|
else if (!enable && !this.isDisabled(plugin)){
|
||||||
|
this.disabled.push(plugin.id);
|
||||||
|
plugin.obj.disabled();
|
||||||
|
}
|
||||||
|
else return;
|
||||||
|
|
||||||
setState: function(identifier, enable){
|
if (plugin.obj.$pluginSettings.requiresPageReload){
|
||||||
var plugin = PLUGINS.findObject(identifier);
|
window.location.reload();
|
||||||
|
}
|
||||||
if (enable && PLUGINS.isDisabled(plugin)){
|
|
||||||
PLUGINS.disabled.splice(PLUGINS.disabled.indexOf(identifier),1);
|
|
||||||
plugin.obj.enabled();
|
|
||||||
PLUGINS.runWhenReady(plugin);
|
|
||||||
}
|
}
|
||||||
else if (!enable && !PLUGINS.isDisabled(plugin)){
|
|
||||||
PLUGINS.disabled.push(identifier);
|
onReady(){
|
||||||
plugin.obj.disabled();
|
this.waiting.forEach(plugin => plugin.obj.ready());
|
||||||
|
this.waiting = [];
|
||||||
}
|
}
|
||||||
else return;
|
};
|
||||||
|
|
||||||
if (plugin.obj.$pluginSettings.requiresPageReload){
|
//
|
||||||
window.location.reload();
|
// Block: Setup global function to change plugin state.
|
||||||
}
|
//
|
||||||
}
|
window.TDPF_setPluginState = function(identifier, enable){
|
||||||
};
|
window.TD_PLUGINS.setState(window.TD_PLUGINS.findObject(identifier),enable);
|
||||||
|
};
|
||||||
window.TD_PLUGINS = PLUGINS;
|
})();
|
||||||
|
|
||||||
if (isReloading){
|
|
||||||
window.location.reload();
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user