Skip to content

Add the specifying the position of the right-click version #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions content/en/snippets/mouse/right_click.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,41 @@ if (typeof Event === "function") {
/* Fire a right-click event */
element.dispatchEvent(event);
```

If the above snippet doesn’t work, please try the below snippet, which specifies the position of the right-click.

\* This snippet does not support IE11.

```js
// Specify the element selector
var selector = "<TODO: REPLACE SELECTOR>";

/* --------- You don't need to touch below ---------- */

// Locate the element
var element = document.querySelector(selector);

// Exit if it does not exist
if (!element) {
throw new Error(
"Error: cannot find the element with selector(" + selector + ")."
);
}

var rect = element.getBoundingClientRect();

// Get the coordinates of the point to be clicked
var contextMenuX = rect.left + rect.width / 2;
var contextMenuY = rect.top + rect.height / 2;

// Create and init a right-click (to be exact, contextmenu) event
var contextMenuEvent = new MouseEvent('contextmenu', {
bubbles: true,
cancelable: true,
clientX: contextMenuX,
clientY: contextMenuY
});

// Fire a right-click event
element.dispatchEvent(contextMenuEvent);
```
38 changes: 38 additions & 0 deletions content/ja/snippets/mouse/right_click.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,41 @@ if (typeof Event === "function") {
/* 右クリックのイベントを発火させる */
element.dispatchEvent(event);
```

上記のスニペットでうまくいかない場合は、右クリックする箇所の座標を指定する以下のスニペットを試してください。

※このスニペットはIE11をサポートしていません。

```js
// 要素のセレクタを指定する
var selector = "<TODO: REPLACE SELECTOR>";

/* --------- ここから下は変える必要はありません ---------- */

// 要素を探す
var element = document.querySelector(selector);

// 要素がなければ処理を中断する
if (!element) {
throw new Error(
"Error: cannot find the element with selector(" + selector + ")."
);
}

var rect = element.getBoundingClientRect();

// クリック箇所の座標を取得する
var contextMenuX = rect.left + rect.width / 2;
var contextMenuY = rect.top + rect.height / 2;

// 右クリック (厳密にはcontextmenu) のイベントを生成して初期化する
var contextMenuEvent = new MouseEvent('contextmenu', {
bubbles: true,
cancelable: true,
clientX: contextMenuX,
clientY: contextMenuY
});

// 右クリックのイベントを発火させる
element.dispatchEvent(contextMenuEvent);
```