1
0
mirror of https://github.com/chylex/TweetDuck.git synced 2025-04-14 12:15:48 +02:00

Add session data that persists across browser reloads

This commit is contained in:
chylex 2017-08-05 18:08:22 +02:00
parent 184340f400
commit 54ac54aba6
3 changed files with 28 additions and 1 deletions
Core
Resources/Scripts

View File

@ -1,8 +1,12 @@
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using CefSharp;
using TweetDuck.Core.Controls;
using TweetDuck.Core.Notification;
using TweetDuck.Core.Other;
using TweetDuck.Core.Utils;
using TweetDuck.Resources;
namespace TweetDuck.Core.Bridge{
sealed class TweetDeckBridge{
@ -11,12 +15,26 @@ sealed class TweetDeckBridge{
public static string LastHighlightedTweet = string.Empty;
public static string LastHighlightedQuotedTweet = string.Empty;
public static string[] LastHighlightedTweetImages = StringUtils.EmptyArray;
public static Dictionary<string, string> SessionData = new Dictionary<string, string>(2);
public static void ResetStaticProperties(){
LastRightClickedLink = LastRightClickedImage = LastHighlightedTweet = LastHighlightedQuotedTweet = string.Empty;
LastHighlightedTweetImages = StringUtils.EmptyArray;
}
public static void RestoreSessionData(IFrame frame){
if (SessionData.Count > 0){
StringBuilder build = new StringBuilder().Append("window.TD_SESSION={");
foreach(KeyValuePair<string, string> kvp in SessionData){
build.Append(kvp.Key).Append(":'").Append(kvp.Value.Replace("'", "\\'")).Append("',");
}
ScriptLoader.ExecuteScript(frame, build.Append("}").ToString(), "gen:session");
SessionData.Clear();
}
}
private readonly FormBrowser form;
private readonly FormNotificationMain notification;
@ -80,6 +98,12 @@ public void DisplayTooltip(string text, bool showInNotification){
}
}
public void SetSessionData(string key, string value){
form.InvokeSafe(() => { // do not use InvokeAsyncSafe, return only after invocation
SessionData.Add(key, value);
});
}
public void LoadNextNotification(){
notification.InvokeAsyncSafe(notification.FinishCurrentNotification);
}

View File

@ -204,6 +204,7 @@ private void browser_FrameLoadEnd(object sender, FrameLoadEndEventArgs e){
e.Frame.ExecuteJavaScriptAsync(TwitterUtils.BackgroundColorFix);
UpdateProperties(PropertyBridge.Environment.Browser);
TweetDeckBridge.RestoreSessionData(e.Frame);
ScriptLoader.ExecuteFile(e.Frame, "code.js");
ReinjectCustomCSS(Config.CustomBrowserCSS);

View File

@ -908,6 +908,8 @@
onAppReady.forEach(func => func());
onAppReady = null;
delete window.TD_SESSION;
if (window.TD_PLUGINS){
window.TD_PLUGINS.onReady();
}