How to disable view source option in Chromium Embedded?
Asked Answered
E

2

7

Is it possible to disable view source option in Delphi Chromium Embedded ?
I haven't found anything suitable in properties/methods list.

Easement answered 31/5, 2012 at 12:39 Comment(0)
A
13

There are no direct settings or events allowing to hide Chromium popup menu items. However you have at least few options how to continue, you can for instance:

1. Tell user that the View source option is forbidden and decline the action

You can decide what action will you allow or decline in the OnMenuAction event handler, where if you assign True to the Result parameter the action is declined. The following code checks that you have performed the view source action and if so, decline the action and show the information message:

type
  TCefMenuId = TCefHandlerMenuId;

procedure TForm1.Chromium1MenuAction(Sender: TObject;
  const browser: ICefBrowser; menuId: TCefMenuId; out Result: Boolean);
begin
  if menuId = MENU_ID_VIEWSOURCE then
  begin
    Result := True;
    ShowMessage('View page source is not allowed!');
  end;
end;

2. Fake the menu item to something custom by changing menu item's caption with its action

You can take advantage of the menu item for something else by changing the menu item's caption and executing some custom action. The following sample code shows how to change the view source menu item into the about box menu item:

type
  TCefMenuId = TCefHandlerMenuId;

procedure TForm1.Chromium1GetMenuLabel(Sender: TObject;
  const browser: ICefBrowser; menuId: TCefMenuId; var caption: ustring;
  out Result: Boolean);
begin
  if menuId = MENU_ID_VIEWSOURCE then
    caption := 'About my application...';
end;

procedure TForm1.Chromium1MenuAction(Sender: TObject;
  const browser: ICefBrowser; menuId: TCefMenuId; out Result: Boolean);
begin
  if menuId = MENU_ID_VIEWSOURCE then
  begin
    Result := True;
    ShowMessage('About box...!');
  end;
end;

3. Create you own custom page (frame) popup menu

You can create your own popup menu, but you need to consider that this menu is quite hardcoded, so you will need to maintain it if you'll need to have it the same with each new version of Delphi Chromium wrapper. Here is the code how to create the page menu without view source menu item:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Menus, cefvcl, ceflib;

type
  PCefMenuInfo = PCefHandlerMenuInfo;

type
  TForm1 = class(TForm)
    Chromium1: TChromium;
    procedure FormCreate(Sender: TObject);
    procedure Chromium1BeforeMenu(Sender: TObject; const browser: ICefBrowser;
      const menuInfo: PCefMenuInfo; out Result: Boolean);
  private
    PageMenu: TPopupMenu;
    procedure OnNavigateBackMenuItemClick(Sender: TObject);
    procedure OnNavigateForwardMenuItemClick(Sender: TObject);
    procedure OnPrintMenuItemClick(Sender: TObject);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.OnNavigateBackMenuItemClick(Sender: TObject);
begin
  Chromium1.Browser.GoBack;
end;

procedure TForm1.OnNavigateForwardMenuItemClick(Sender: TObject);
begin
  Chromium1.Browser.GoForward;
end;

procedure TForm1.OnPrintMenuItemClick(Sender: TObject);
begin
  Chromium1.Browser.GetFocusedFrame.Print;
end;

procedure TForm1.Chromium1BeforeMenu(Sender: TObject;
  const browser: ICefBrowser; const menuInfo: PCefMenuInfo;
  out Result: Boolean);
begin
  if menuInfo.typeFlags = MENUTYPE_PAGE then
  begin
    Result := True;
    PageMenu.Items[0].Enabled := browser.CanGoBack;
    PageMenu.Items[1].Enabled := browser.CanGoForward;
    PageMenu.Popup(menuInfo^.x, menuInfo^.y);
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  MenuItem: TMenuItem;
begin
  PageMenu := TPopupMenu.Create(Self);
  MenuItem := TMenuItem.Create(PageMenu);
  MenuItem.Caption := 'Back';
  MenuItem.OnClick := OnNavigateBackMenuItemClick;
  PageMenu.Items.Add(MenuItem);
  MenuItem := TMenuItem.Create(PageMenu);
  MenuItem.Caption := 'Forward';
  MenuItem.OnClick := OnNavigateForwardMenuItemClick;
  PageMenu.Items.Add(MenuItem);
  MenuItem := TMenuItem.Create(PageMenu);
  MenuItem.Caption := '-';
  PageMenu.Items.Add(MenuItem);
  MenuItem := TMenuItem.Create(PageMenu);
  MenuItem.Caption := 'Print';
  MenuItem.OnClick := OnPrintMenuItemClick;
  PageMenu.Items.Add(MenuItem);
  Chromium1.Load('www.stackoverflow.com');
end;

end.

Footnote

The type definitions used in all code samples are there because I've noticed that some version of Delphi Chromium has wrong event handler definitions.

Anabolite answered 31/5, 2012 at 13:13 Comment(0)
M
2

Probably things changed over years, today a direct method exists:

uses
  ceflib;

[..]

implementation

procedure TForm1.Chromium1BeforeContextMenu(Sender: TObject;
  const browser: ICefBrowser; const frame: ICefFrame;
  const params: ICefContextMenuParams; const model: ICefMenuModel);
begin
  //model.Clear;
  model.Remove(Integer(MENU_ID_VIEW_SOURCE));
end;

You can use model.Clear if you want to completely get rid of popup menu.

Markova answered 16/5, 2017 at 9:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.