Skip to content
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

Add Page Action Setting (icon in URL bar) #931

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 3 additions & 0 deletions src/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@
"message": "Redirect to original",
"description": "Used in context menus when right clicking on a page/tab"
},
"pageAction": {
"message": "Show in Page Action"
},
"redirectLink": {
"message": "Attempt to redirect",
"description": "Used in context menus when right clicking on a hyperlink"
Expand Down
11 changes: 11 additions & 0 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@
"128": "assets/images/libredirect-128.png"
}
},
"page_action": {
"default_title": "__MSG_extensionName__",
"browser_style": false,
"default_popup": "pages/popup/popup.html",
"default_icon": {
"16": "assets/images/libredirect-16.png",
"32": "assets/images/libredirect-32.png",
"48": "assets/images/libredirect-48.png",
"128": "assets/images/libredirect-128.png"
}
},
"options_ui": {
"page": "pages/options/index.html",
"browser_style": false,
Expand Down
7 changes: 7 additions & 0 deletions src/pages/background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ browser.runtime.onInstalled.addListener(async details => {

let tabIdRedirects = {}

// page action
browser.tabs.onUpdated.addListener(async (id, changeInfo, tabInfo) => {
const { pageAction } = await utils.getOptions()
if (!pageAction) return;
browser.pageAction.show(tabInfo.id);
Copy link
Author

@KaceCottam KaceCottam Jun 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
browser.pageAction.show(tabInfo.id);
const url = new URL(tabInfo.url)
await servicesHelper.computeService(url).then(r => {
if (r) {
browser.pageAction.show(id)
}
})

I mean how do you figure it out ?

Currently pageAction is done by the URL not the page's contents

This correctly detects if we are using a service that can be redirected with libredirect. I tested it with reddit, youtube, tenor, and github. There may be some failure points, but it seems to do the trick on the initial test. On websites like HBO max it would not show the page action.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, you are inspecting page data or what ?

I am confused between ID and URL right now.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to inspect page content-- this is solely based on the URL of the tab.
show(id) tells the browser to show the page action (button in the address bar) for that tab identifier:
https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs/Tab#id

It looks like servicesHelper.computeServices can take that url and figure out if it is a reddit link (or one of the alternative reddit types), or if it is a youtube, tenor, etc/alternative service.
It is based on this bit of code: https://github.com/libredirect/browser_extension/blob/32dc67db284b5803c9925df07f2cc47127acfdf6/src/pages/popup/popup.js#L109C3-L115C4

});

// true == Always redirect, false == Never redirect, null/undefined == follow options for services
browser.webRequest.onBeforeRequest.addListener(
details => {
Expand Down
6 changes: 6 additions & 0 deletions src/pages/options/widgets/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ bookmarksMenuElement.addEventListener('change', async event => {
browser.permissions.remove({ permissions: ["bookmarks"] }, r => bookmarksMenuElement.checked = !r)
})

const pageActionElement = document.getElementById('pageAction')
pageActionElement.addEventListener('change', event => {
setOption('pageAction', 'checkbox', event)
})

let themeElement = document.getElementById("theme")
themeElement.addEventListener("change", event => {
setOption("theme", "select", event)
Expand Down Expand Up @@ -149,6 +154,7 @@ let options = await utils.getOptions()
themeElement.value = options.theme
fetchInstancesElement.value = options.fetchInstances
redirectOnlyInIncognitoElement.checked = options.redirectOnlyInIncognito
pageActionElement.checked = options.pageAction
browser.permissions.contains({ permissions: ["bookmarks"] }, r => bookmarksMenuElement.checked = r)
for (const service in config.services) document.getElementById(service).checked = options.popupServices.includes(service)

Expand Down
4 changes: 4 additions & 0 deletions src/pages/options/widgets/general.pug
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ section(class="block-option" id="general_page")
label(for='bookmarksMenu' data-localise="__MSG_bookmarksMenu__") Bookmarks menu
input(id='bookmarksMenu' type="checkbox")

div(class="block block-option")
label(for='pageAction' data-localise="__MSG_pageAction__") Use Page Action
input(id='pageAction' type="checkbox")

div(class="block block-option")
label(data-localise="__MSG_excludeFromRedirecting__") Excluded from redirecting

Expand Down
Loading