-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Control & middle click to open links in new tabs
- Loading branch information
Showing
4 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
/// <reference path="../link.d.ts" /> | ||
|
||
export type ClickHandlerMessage = { | ||
name: 'openlink' | ||
data: { href: string } | ||
} & { target: JSWindowActorParent } | ||
|
||
export class ClickHandlerChild extends JSWindowActorChild { | ||
getHrefIfExists(target: Node): string | undefined { | ||
if ((target as HTMLAnchorElement).href) { | ||
return (target as HTMLAnchorElement).href | ||
} | ||
|
||
if (target.parentElement) { | ||
return this.getHrefIfExists(target.parentElement) | ||
} | ||
} | ||
|
||
handleEvent(event: MouseEvent) { | ||
if (event.defaultPrevented || !event.target) return | ||
|
||
const href = this.getHrefIfExists(event.target as Node) | ||
|
||
const ctrlClick = event.button === 0 && event.ctrlKey | ||
const middleClick = event.button === 1 | ||
|
||
const shouldOpenNewTab = href && (ctrlClick || middleClick) | ||
|
||
if (!shouldOpenNewTab) return | ||
|
||
this.sendAsyncMessage('openlink', { href }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
/// <reference path="../link.d.ts" /> | ||
import { ClickHandlerMessage } from './ClickHandlerChild' | ||
|
||
export class ClickHandlerParent extends JSWindowActorParent { | ||
receiveMessage(event: ClickHandlerMessage) { | ||
if (event.name == 'openlink') { | ||
const win = event.target.browsingContext.embedderElement.ownerGlobal | ||
const uri = Services.io.newURI(event.data.href) | ||
win.windowApi.tabs.openTab(uri) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
[ | ||
"ClickHandlerChild", | ||
"ClickHandlerParent", | ||
"ContextMenuChild", | ||
"ContextMenuParent", | ||
"LinkHandlerChild", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters