|
1 | | -import React, { FunctionComponent } from "react"; |
| 1 | +import React from "react"; |
2 | 2 | import { Hello } from "@src/components/hello"; |
3 | | -import { browser } from "webextension-polyfill-ts"; |
| 3 | +import { browser, Tabs } from "webextension-polyfill-ts"; |
4 | 4 | import { Scroller } from "@src/components/scroller"; |
5 | | -import "./styles.scss"; |
| 5 | +import css from "./styles.module.css"; |
6 | 6 |
|
7 | 7 | // // // // |
8 | 8 |
|
9 | | -export const Popup: FunctionComponent = () => { |
| 9 | +// Scripts to execute in current tab |
| 10 | +const scrollToTopScript = `window.scroll(0,0)`; |
| 11 | +const scrollToBottomScript = `window.scroll(0,9999999)`; |
| 12 | + |
| 13 | +/** |
| 14 | + * Executes a string of Javascript on the current tab |
| 15 | + * @param code The string of code to execute on the current tab |
| 16 | + */ |
| 17 | +function executeScript(code: string): void { |
| 18 | + // Query for the active tab in the current window |
| 19 | + browser.tabs |
| 20 | + .query({ active: true, currentWindow: true }) |
| 21 | + .then((tabs: Tabs.Tab[]) => { |
| 22 | + // Pulls current tab from browser.tabs.query response |
| 23 | + const currentTab: Tabs.Tab | undefined = tabs[0]; |
| 24 | + |
| 25 | + // Short circuits function execution is current tab isn't found |
| 26 | + if (!currentTab) { |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + // Executes the script in the current tab |
| 31 | + browser.tabs |
| 32 | + .executeScript(currentTab.id, { |
| 33 | + code, |
| 34 | + }) |
| 35 | + .then(() => { |
| 36 | + console.log("Done Scrolling"); |
| 37 | + }); |
| 38 | + }); |
| 39 | +} |
| 40 | + |
| 41 | +// // // // |
| 42 | + |
| 43 | +export function Popup() { |
10 | 44 | // Sends the `popupMounted` event |
11 | 45 | React.useEffect(() => { |
12 | 46 | browser.runtime.sendMessage({ popupMounted: true }); |
13 | 47 | }, []); |
14 | 48 |
|
15 | 49 | // Renders the component tree |
16 | 50 | return ( |
17 | | - <div className="popup-container"> |
18 | | - <div className="container mx-4 my-4"> |
| 51 | + <div className={css.popupContainer}> |
| 52 | + <div className="mx-4 my-4"> |
19 | 53 | <Hello /> |
20 | 54 | <hr /> |
21 | | - <Scroller /> |
| 55 | + <Scroller |
| 56 | + onClickScrollTop={() => { |
| 57 | + executeScript(scrollToTopScript); |
| 58 | + }} |
| 59 | + onClickScrollBottom={() => { |
| 60 | + executeScript(scrollToBottomScript); |
| 61 | + }} |
| 62 | + /> |
22 | 63 | </div> |
23 | 64 | </div> |
24 | 65 | ); |
25 | | -}; |
| 66 | +} |
0 commit comments