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

Allow plugins to inject HTML into notifications before they're shown

Closes 
This commit is contained in:
chylex 2017-04-01 19:27:05 +02:00
parent 8e0c4f5308
commit 1a73fcdb39
3 changed files with 40 additions and 4 deletions

View File

@ -164,12 +164,15 @@ public virtual void ResumeNotification(){
}
}
protected virtual string GetTweetHTML(TweetNotification tweet){
string bodyClasses = browser.Bounds.Contains(PointToClient(Cursor.Position)) ? "td-hover" : string.Empty;
return tweet.GenerateHtml(bodyClasses);
}
protected virtual void LoadTweet(TweetNotification tweet){
CurrentUrl = tweet.Url;
CurrentQuotedTweetUrl = string.Empty; // load from JS
string bodyClasses = browser.Bounds.Contains(PointToClient(Cursor.Position)) ? "td-hover" : string.Empty;
browser.LoadHtml(tweet.GenerateHtml(bodyClasses), "http://tweetdeck.twitter.com/?"+DateTime.Now.Ticks);
browser.LoadHtml(GetTweetHTML(tweet), "http://tweetdeck.twitter.com/?"+DateTime.Now.Ticks);
}
protected virtual void SetNotificationSize(int width, int height){

View File

@ -200,6 +200,16 @@ public override void ResumeNotification(){
}
}
protected override string GetTweetHTML(TweetNotification tweet){
string html = base.GetTweetHTML(tweet);
foreach(InjectedHTML injection in plugins.Bridge.NotificationInjections){
html = injection.Inject(html);
}
return html;
}
protected override void LoadTweet(TweetNotification tweet){
timerProgress.Stop();
totalTime = timeLeft = tweet.GetDisplayDuration(Program.UserConfig.NotificationDurationValue);

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using TweetDck.Core.Utils;
@ -13,6 +14,13 @@ private static string SanitizeCacheKey(string key){
private readonly PluginManager manager;
private readonly TwoKeyDictionary<int, string, string> fileCache = new TwoKeyDictionary<int, string, string>(4, 2);
private readonly TwoKeyDictionary<int, string, InjectedHTML> notificationInjections = new TwoKeyDictionary<int,string,InjectedHTML>(4, 1);
public IEnumerable<InjectedHTML> NotificationInjections{
get{
return notificationInjections.InnerValues;
}
}
public PluginBridge(PluginManager manager){
this.manager = manager;
@ -20,16 +28,23 @@ public PluginBridge(PluginManager manager){
this.manager.PluginChangedState += manager_PluginChangedState;
}
// Event handlers
private void manager_Reloaded(object sender, PluginErrorEventArgs e){
fileCache.Clear();
}
private void manager_PluginChangedState(object sender, PluginChangedStateEventArgs e){
if (!e.IsEnabled){
fileCache.Remove(manager.GetTokenFromPlugin(e.Plugin));
int token = manager.GetTokenFromPlugin(e.Plugin);
fileCache.Remove(token);
notificationInjections.Remove(token);
}
}
// Utility methods
private string GetFullPathOrThrow(int token, PluginFolder folder, string path){
Plugin plugin = manager.GetPluginFromToken(token);
string fullPath = plugin == null ? string.Empty : plugin.GetFullPathIfSafe(folder, path);
@ -98,5 +113,13 @@ public string ReadFileRoot(int token, string path){
public bool CheckFileExistsRoot(int token, string path){
return File.Exists(GetFullPathOrThrow(token, PluginFolder.Root, path));
}
public void InjectIntoNotificationsBefore(int token, string key, string search, string html){
notificationInjections[token, key] = new InjectedHTML(InjectedHTML.Position.Before, search, html);
}
public void InjectIntoNotificationsAfter(int token, string key, string search, string html){
notificationInjections[token, key] = new InjectedHTML(InjectedHTML.Position.After, search, html);
}
}
}