-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from jishaal/wip-next
Refactor and Reddit support
- Loading branch information
Showing
19 changed files
with
349 additions
and
193 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 |
---|---|---|
@@ -1,9 +1,14 @@ | ||
# Night Owl (Chrome Extension) | ||
|
||
🦉 An extension to save your eyes! Automatically make Twitter's night mode turn on and off at sunset and sunrise. | ||
🦉 An extension to save your eyes! Automatically make your favourite website's night mode turn on and off at sunset and sunrise. | ||
|
||
The following sites are currently supported | ||
|
||
- twitter.com | ||
- reddit.com | ||
|
||
[Get the latest version from the Chrome Store](https://chrome.google.com/webstore/detail/night-owl/iagomahcookmbnimmomjcmiaimmhompf) | ||
|
||
--- | ||
|
||
![TwitterScreenshot](./screenshots/screenshot.JPG) | ||
![TwitterScreenshot](./screenshots/screenshot.png) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
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
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
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 +1,2 @@ | ||
export const TWITTER_ON = 'userTwitterOn'; | ||
export const REDDIT_ON = 'userRedditOn'; |
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,13 +1,52 @@ | ||
// This file is injected as a content script | ||
console.log('Hello from content script!'); | ||
(function () { | ||
const userDropDownNode = document.querySelector('#USER_DROPDOWN_ID'); | ||
|
||
// https://github.com/sindresorhus/element-ready | ||
// click the button | ||
// assert if on and do night mode logic | ||
// there is a JWT in local storage USER that could also be read from | ||
// if we want more progmattic logic checking rather than clicks | ||
|
||
// userDropDownNode.click(); | ||
})(); | ||
import elementReady from 'element-ready'; | ||
import browser from 'webextension-polyfill'; | ||
import { NightBrowserMessage } from './types'; | ||
|
||
console.log('content loaded'); | ||
|
||
async function toggleReddit(isNight: boolean) { | ||
const dropdown = await elementReady('#USER_DROPDOWN_ID'); | ||
if (dropdown) { | ||
dropdown.click(); | ||
const menu = await elementReady('[role="menu"]'); | ||
const menuItems = menu?.children[0].children ? Array.from(menu?.children[0].children) : []; | ||
|
||
if (menuItems.length) { | ||
const viewOptionsHeader = menuItems.filter((e) => e.textContent === 'View Options')[0]; | ||
if (viewOptionsHeader) { | ||
const darkModeContainer = viewOptionsHeader.nextElementSibling as HTMLElement; | ||
const darkModeSwitch = darkModeContainer.querySelector( | ||
'[role="switch"]', | ||
) as HTMLElement; | ||
|
||
// No switch if the user is not logged in | ||
if (!darkModeSwitch) { | ||
dropdown.click(); | ||
} | ||
|
||
const isRedditDarkModeEnabled = | ||
darkModeSwitch?.getAttribute('aria-checked') === 'true'; | ||
|
||
// TODO: This is a hacky way to toggle the switch, but it works for now | ||
if ( | ||
(isNight && !isRedditDarkModeEnabled) || | ||
(!isNight && isRedditDarkModeEnabled) | ||
) { | ||
darkModeSwitch.click(); | ||
dropdown.click(); | ||
} else { | ||
dropdown.click(); | ||
} | ||
} else { | ||
// User isn't logged in, close the dropdown | ||
dropdown.click(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
browser.runtime.onMessage.addListener((request: NightBrowserMessage) => { | ||
if (request.type === 'redditIsNight' && request.isUserEnabled) { | ||
toggleReddit(request.value); | ||
} | ||
}); |
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 |
---|---|---|
|
@@ -2,5 +2,3 @@ declare module '*.svg' { | |
const content: any; | ||
export default content; | ||
} | ||
|
||
declare var browser: any; |
Oops, something went wrong.