fregante / webext-permission-toggle

Browser-action context menu to request permission for the current tab.
https://fregante.github.io/webext-permission-toggle/
MIT License
70 stars 5 forks source link

Add Safari support (done) #10

Closed fregante closed 3 years ago

fregante commented 3 years ago

Safari has its own permission system which would replace this module

However this module should be updated to avoid adding a non-working context menu item

fregante commented 3 years ago

I found out why: tab.url is an empty string for some reason. activeTab should fill that in.

https://github.com/fregante/webext-domain-permission-toggle/blob/2aacb195e7fbaaf04e23c32b074b5bbd10651732/index.ts#L88-L92

If this can be fixed, I think this module is still the easiest way to build cross-browser extensions since we don't have to customize the Safari build.

fregante commented 3 years ago

tab.url is empty because apparently context menu clicks don't "activate" the tab (activeTab permission). Left-clicking the browserAction first and then clicking the context menu item works, but that's awkward.

Screen Shot 2

This bug might be solved by Safari in the near future or it might not.


I have 2 "solutions" for this:

Perhaps I use both:

fregante commented 3 years ago

Example:

Screen Shot 3
if (!tab.url) {
    browser.tabs.executeScript(tab.id, {
        code: 'alert("To enable this site:\\n1. Open Safari\'s Preferences\\n2. Visit the \\"Extensions\\" tab\\n3. Select this extension\\n4. Click \\"Edit Websites...\\"")'
    });
    return;
}
fregante commented 3 years ago

I think I found a workable solution!!

Step 1: Ask the user to try again.

if (!tab.url) {
    chrome.tabs.executeScript({
        code: `alert("Try again pretty please?")` // Alert doesn't work from background in Safari
    });
    return;
}

Step 2: Bind the request/remove functions to chrome.permissions because that's how Safari likes them.

Step 3: Make sure that the menuitem checkmark is updated correctly when it fails

🥳

However webext-dynamic-content-scripts also needs to be fixed.