Replies: 11 comments
-
Ok, just an update, I manage to come up with the following:
The only thing right now is that copyImageLink, copyLink, writingDirection and getImageDescriptionsFromMicrosoft are still showing. What would be the correct way to disable the QR code option (there are a few)? Also is there a better way that the above can be written? Cheers Edit: silly code editor borking my code, how rude GitHub! |
Beta Was this translation helpful? Give feedback.
-
@nishitha-burman Is @yildirimcagri going to get back to this? I know it's the holiday season now but I had this opened like 3 weeks ago. |
Beta Was this translation helpful? Give feedback.
-
Hi @Danielx64 , apologies for late reply, I am currently on vacation, but I'll try to reply based on my knowledge. If you are trying to get rid of most items, you could also consider having the check for items you want to retain instead and remove others by default. The items are names in camelCase, and in general by their text in English, and you can look at each item's name for the ones you want to retain. One thing to keep in mind, if there are some items that are named as 'unknown' that's because there is some new context menu item that we haven't added a name for (this may be a bug in our release process where we cannot know an item added by Chromium, but this should be less likely now). For those, you'd need to identify them by the 'commandId' field. Are you seeing the name reported as 'unknown' for the items that are not being cleared? |
Beta Was this translation helpful? Give feedback.
-
I'm not sure about the unknown name at all. I like the idea of checking for what I want to keep and removing the others. I guess (not tried yet) something like this would work:
|
Beta Was this translation helpful? Give feedback.
-
Ok I just tested my theory and that didn't work, back to square one |
Beta Was this translation helpful? Give feedback.
-
That code has the correct idea. Could you share your full code for 'ContextMenuRequested' handler to see why it wouldn't work? |
Beta Was this translation helpful? Give feedback.
-
Sorry for not getting back sooner, been away and back now. This is what I currently have:
Now what I meant that it didn't work was that it got rid of stuff that I couldn't before but it also didn't keep what I wanted and brought back things that I could get rid of before. |
Beta Was this translation helpful? Give feedback.
-
@yildirimcagri I know it may be still holiday season etc, have you had much of a chance to see what I'm doing wrong? |
Beta Was this translation helpful? Give feedback.
-
Hi @Danielx64 , The conditional needs to be && (if items are none of these then remove) and since the list is being modified as you remove items while being iterated, you should start iterating from the bottom of list, otherwise some items are skipped from being checked. The following code only keeps the copy, paste, cut and refresh items in context menus: for (int index = menuList.Count - 1; index >= 0; index--)
{
if (menuList[index].Name != "copy" && menuList[index].Name != "paste" && menuList[index].Name != "cut" && menuList[index].Name != "refresh") { menuList.RemoveAt(index); }
} Keep in mind that some context menu items are submenus and you'd need to traverse through them if you are interested in keeping any items in submenus. However, from your check the items it seems you only want the top menu items so I didn't include that in the snippet. Another optimization you may be able to do is you can save the menuList once you build it and reuse it in subsequent |
Beta Was this translation helpful? Give feedback.
-
Would it not be more correct to use the CommandId property instead, to ensure support of multiple languages? |
Beta Was this translation helpful? Give feedback.
-
Removing all except private void CoreWebView2_ContextMenuRequested(object? sender, CoreWebView2ContextMenuRequestedEventArgs e)
{
IList<CoreWebView2ContextMenuItem> menuList = e.MenuItems;
CoreWebView2ContextMenuTargetKind context = e.ContextMenuTarget.Kind;
for (int i = 0; i < menuList.Count; i++)
{
var item = menuList[i];
switch (context)
{
case CoreWebView2ContextMenuTargetKind.Page:
CoreWebView2ContextMenuItem? webCapture = menuList.FirstOrDefault(i => i?.Name == "webCapture", null);
menuList.Clear();
if (webCapture != null)
menuList.Add(webCapture);
break;
}
}
} |
Beta Was this translation helpful? Give feedback.
-
Hi there
I'm currently reading the docs on disabling different options in the webview2 context menu (https://learn.microsoft.com/en-us/microsoft-edge/webview2/how-to/context-menus?tabs=csharp#example-removing-menu-items-from-a-default-context-menu).
While it has an example of how to disable the "save image as" option, can we please get details about what the correct keyword to disable other options? An example is when you right-click on a page, there's a generate "QR code" option or disabling the share option.
Also, what's the correct way of disabling several options in one go?
Currently, I have "WebView.CoreWebView2.Settings.AreDefaultContextMenusEnabled = false;" and looking to enable it so that people can use spell check and copy/paste in textboxes but I don't want the other options to show up (although I might let refresh show up).
Cheers.
Beta Was this translation helpful? Give feedback.
All reactions