Skip to content

Ability to close a tab is limited to javascript-opened or "⌘-click" opened #310

@TS6ix2GaH3q679h5

Description

@TS6ix2GaH3q679h5

I'd like to have a script that lets me close the currently open tab when I click in a specific part of the page.

The problem is, using

close();

in a script is unreliable. It can only close tabs that either were opened by Javascript to begin with, or tabs that I manually "Open in new tab" opened, be it from the context menu or a ⌘-click action.

I've tried adding the following:

// @grant        window.close
// @grant      close
// @grant       GM_addStyle

But no luck there either. As I understand it, this is a security issue that doesn't allow page scripts to close tabs. Indeed, when I run my user script, the permissions seem ok:

close tab test 3.js @inject-into value changed due to @grant values

But when I try it in standard Safari tabs, console logs:

Can't close the window since it was not opened by JavaScript

Is there some way the Userscript extension can use it's browser extension status, existing as it does "above" any page scripts, to permit this behavior? A Chrome extension I also use "Close this Tab" can do something very similar. It's likely because of it's privileges as an extension rather than just a user script. I'd love to be able to use that close command on any active tab.

Thank you

System Information:

macOS or iOS version: macOS 12.5
Userscripts version: 4.2.3
Safari version: 15.6
Is this issue related to script injection? Sort of
Did the test script (pasted above) successfully run on your machine? Yes

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    Status

    Release

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions