mirror of
https://github.com/chylex/TweetDuck.git
synced 2025-04-11 12:15:44 +02:00
Refactor context menu link handling
This commit is contained in:
parent
561aec5ef0
commit
94946a9ed6
@ -54,24 +54,27 @@ public virtual void OnBeforeContextMenu(IWebBrowser browserControl, IBrowser bro
|
||||
else{
|
||||
LastLink = TweetDeckBridge.ContextInfo.Link;
|
||||
LastChirp = TweetDeckBridge.ContextInfo.Chirp;
|
||||
}
|
||||
|
||||
if (LastLink.Type == ContextInfo.LinkType.Unknown){
|
||||
LastLink = new ContextInfo.LinkInfo(parameters);
|
||||
}
|
||||
}
|
||||
|
||||
if (parameters.TypeFlags.HasFlag(ContextMenuType.Selection) && !parameters.TypeFlags.HasFlag(ContextMenuType.Editable)){
|
||||
model.AddItem(MenuSearchInBrowser, "Search in browser");
|
||||
model.AddSeparator();
|
||||
model.AddItem(MenuReadApplyROT13, "Apply ROT13");
|
||||
model.AddSeparator();
|
||||
}
|
||||
|
||||
bool hasTweetImage = LastLink.IsImage;
|
||||
bool hasTweetVideo = LastLink.IsVideo;
|
||||
|
||||
|
||||
string TextOpen(string name) => "Open "+name+" in browser";
|
||||
string TextCopy(string name) => "Copy "+name+" address";
|
||||
string TextSave(string name) => "Save "+name+" as...";
|
||||
|
||||
ContextInfo.LinkType type = LastLink.Type;
|
||||
|
||||
if (parameters.TypeFlags.HasFlag(ContextMenuType.Link) && !parameters.UnfilteredLinkUrl.EndsWith("tweetdeck.twitter.com/#", StringComparison.Ordinal) && !hasTweetImage && !hasTweetVideo){
|
||||
if (TwitterUtils.RegexAccount.IsMatch(parameters.UnfilteredLinkUrl)){
|
||||
if (type == ContextInfo.LinkType.Generic && !LastLink.UnsafeUrl.EndsWith("tweetdeck.twitter.com/#", StringComparison.Ordinal)){
|
||||
if (TwitterUtils.RegexAccount.IsMatch(LastLink.UnsafeUrl)){
|
||||
model.AddItem(MenuOpenLinkUrl, TextOpen("account"));
|
||||
model.AddItem(MenuCopyLinkUrl, TextCopy("account"));
|
||||
model.AddItem(MenuCopyUsername, "Copy account username");
|
||||
@ -83,14 +86,13 @@ public virtual void OnBeforeContextMenu(IWebBrowser browserControl, IBrowser bro
|
||||
|
||||
model.AddSeparator();
|
||||
}
|
||||
|
||||
if (hasTweetVideo){
|
||||
else if (type == ContextInfo.LinkType.Video){
|
||||
model.AddItem(MenuOpenMediaUrl, TextOpen("video"));
|
||||
model.AddItem(MenuCopyMediaUrl, TextCopy("video"));
|
||||
model.AddItem(MenuSaveMedia, TextSave("video"));
|
||||
model.AddSeparator();
|
||||
}
|
||||
else if (((parameters.TypeFlags.HasFlag(ContextMenuType.Media) && parameters.HasImageContents) || hasTweetImage) && parameters.SourceUrl != TweetNotification.AppLogo.Url){
|
||||
else if (type == ContextInfo.LinkType.Image && LastLink.Url != TweetNotification.AppLogo.Url){
|
||||
model.AddItem(MenuViewImage, "View image in photo viewer");
|
||||
model.AddItem(MenuOpenMediaUrl, TextOpen("image"));
|
||||
model.AddItem(MenuCopyMediaUrl, TextCopy("image"));
|
||||
@ -109,25 +111,28 @@ public virtual bool OnContextMenuCommand(IWebBrowser browserControl, IBrowser br
|
||||
|
||||
switch(commandId){
|
||||
case MenuOpenLinkUrl:
|
||||
OpenBrowser(control, LastLink.GetUrl(parameters, true));
|
||||
OpenBrowser(control, LastLink.Url);
|
||||
break;
|
||||
|
||||
case MenuCopyLinkUrl:
|
||||
SetClipboardText(control, LastLink.GetUrl(parameters, false));
|
||||
SetClipboardText(control, LastLink.UnsafeUrl);
|
||||
break;
|
||||
|
||||
case MenuCopyUsername:
|
||||
Match match = TwitterUtils.RegexAccount.Match(parameters.UnfilteredLinkUrl);
|
||||
SetClipboardText(control, match.Success ? match.Groups[1].Value : parameters.UnfilteredLinkUrl);
|
||||
case MenuCopyUsername: {
|
||||
string url = LastLink.UnsafeUrl;
|
||||
Match match = TwitterUtils.RegexAccount.Match(url);
|
||||
|
||||
SetClipboardText(control, match.Success ? match.Groups[1].Value : url);
|
||||
control.InvokeAsyncSafe(analytics.AnalyticsFile.CopiedUsernames.Trigger);
|
||||
break;
|
||||
}
|
||||
|
||||
case MenuOpenMediaUrl:
|
||||
OpenBrowser(control, TwitterUtils.GetMediaLink(LastLink.GetMediaSource(parameters), ImageQuality));
|
||||
OpenBrowser(control, TwitterUtils.GetMediaLink(LastLink.Url, ImageQuality));
|
||||
break;
|
||||
|
||||
case MenuCopyMediaUrl:
|
||||
SetClipboardText(control, TwitterUtils.GetMediaLink(LastLink.GetMediaSource(parameters), ImageQuality));
|
||||
SetClipboardText(control, TwitterUtils.GetMediaLink(LastLink.Url, ImageQuality));
|
||||
break;
|
||||
|
||||
case MenuViewImage: {
|
||||
@ -142,7 +147,7 @@ void ViewImage(string path){
|
||||
}
|
||||
}
|
||||
|
||||
string url = LastLink.GetMediaSource(parameters);
|
||||
string url = LastLink.Url;
|
||||
string file = Path.Combine(BrowserCache.CacheFolder, TwitterUtils.GetImageFileName(url) ?? Path.GetRandomFileName());
|
||||
|
||||
control.InvokeAsyncSafe(() => {
|
||||
@ -164,8 +169,8 @@ void ViewImage(string path){
|
||||
}
|
||||
|
||||
case MenuSaveMedia: {
|
||||
bool isVideo = LastLink.IsVideo;
|
||||
string url = LastLink.GetMediaSource(parameters);
|
||||
bool isVideo = LastLink.Type == ContextInfo.LinkType.Video;
|
||||
string url = LastLink.Url;
|
||||
string username = LastChirp.Authors.LastOrDefault();
|
||||
|
||||
control.InvokeAsyncSafe(() => {
|
||||
|
@ -25,25 +25,46 @@ public void Reset(){
|
||||
|
||||
// Data structures
|
||||
|
||||
public enum LinkType{
|
||||
Unknown, Generic, Image, Video
|
||||
}
|
||||
|
||||
public struct LinkInfo{
|
||||
public bool IsLink => type == "link";
|
||||
public bool IsImage => type == "image";
|
||||
public bool IsVideo => type == "video";
|
||||
public LinkType Type { get; }
|
||||
|
||||
public string GetUrl(IContextMenuParams parameters, bool safe){
|
||||
return IsLink ? url : (safe ? parameters.LinkUrl : parameters.UnfilteredLinkUrl);
|
||||
}
|
||||
|
||||
public string GetMediaSource(IContextMenuParams parameters){
|
||||
return IsImage || IsVideo ? url : parameters.SourceUrl;
|
||||
}
|
||||
|
||||
private readonly string type;
|
||||
private readonly string url;
|
||||
public string Url { get; }
|
||||
public string UnsafeUrl { get; }
|
||||
|
||||
public LinkInfo(string type, string url){
|
||||
this.type = type;
|
||||
this.url = url;
|
||||
switch(type){
|
||||
case "link": Type = LinkType.Generic; break;
|
||||
case "image": Type = LinkType.Image; break;
|
||||
case "video": Type = LinkType.Video; break;
|
||||
default: Type = LinkType.Unknown; break;
|
||||
}
|
||||
|
||||
Url = url;
|
||||
UnsafeUrl = url;
|
||||
}
|
||||
|
||||
public LinkInfo(IContextMenuParams parameters){
|
||||
ContextMenuType type = parameters.TypeFlags;
|
||||
|
||||
if (type.HasFlag(ContextMenuType.Media) && parameters.HasImageContents){
|
||||
Type = LinkType.Image;
|
||||
Url = parameters.SourceUrl;
|
||||
UnsafeUrl = parameters.SourceUrl;
|
||||
}
|
||||
else if (type.HasFlag(ContextMenuType.Link)){
|
||||
Type = LinkType.Generic;
|
||||
Url = parameters.LinkUrl;
|
||||
UnsafeUrl = parameters.UnfilteredLinkUrl;
|
||||
}
|
||||
else{
|
||||
Type = LinkType.Unknown;
|
||||
Url = string.Empty;
|
||||
UnsafeUrl = string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -640,7 +640,7 @@
|
||||
else if (me.classList.contains("js-gif-play")){
|
||||
$TD.setRightClickedLink("video", $(this).closest(".js-media-gif-container").find("video").attr("src"));
|
||||
}
|
||||
else{
|
||||
else if (me.hasAttribute("data-full-url")){
|
||||
$TD.setRightClickedLink("link", me.getAttribute("data-full-url"));
|
||||
}
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user