11import React from "react" ;
22import { Hello } from "@src/components/hello" ;
3- import { browser , Tabs } from "webextension-polyfill-ts " ;
3+ import browser , { Tabs } from "webextension-polyfill" ;
44import { Scroller } from "@src/components/scroller" ;
55import css from "./styles.module.css" ;
66
77// // // //
88
99// Scripts to execute in current tab
10- const scrollToTopScript = `window.scroll(0,0)` ;
11- const scrollToBottomScript = `window.scroll(0,9999999)` ;
10+ const scrollToTopPosition = 0 ;
11+ const scrollToBottomPosition = 9999999 ;
12+
13+ function scrollWindow ( position : number ) {
14+ window . scroll ( 0 , position ) ;
15+ }
1216
1317/**
1418 * Executes a string of Javascript on the current tab
1519 * @param code The string of code to execute on the current tab
1620 */
17- function executeScript ( code : string ) : void {
21+ function executeScript ( position : number ) : void {
1822 // Query for the active tab in the current window
1923 browser . tabs
2024 . query ( { active : true , currentWindow : true } )
2125 . then ( ( tabs : Tabs . Tab [ ] ) => {
2226 // Pulls current tab from browser.tabs.query response
23- const currentTab : Tabs . Tab | undefined = tabs [ 0 ] ;
27+ const currentTab : Tabs . Tab | number = tabs [ 0 ] ;
2428
2529 // Short circuits function execution is current tab isn't found
2630 if ( ! currentTab ) {
2731 return ;
2832 }
33+ const currentTabId : number = currentTab . id as number ;
2934
3035 // Executes the script in the current tab
31- browser . tabs
32- . executeScript ( currentTab . id , {
33- code,
36+ browser . scripting
37+ . executeScript ( {
38+ target : {
39+ tabId : currentTabId
40+ } ,
41+ func : scrollWindow ,
42+ args : [ position ]
3443 } )
3544 . then ( ( ) => {
3645 console . log ( "Done Scrolling" ) ;
@@ -54,10 +63,10 @@ export function Popup() {
5463 < hr />
5564 < Scroller
5665 onClickScrollTop = { ( ) => {
57- executeScript ( scrollToTopScript ) ;
66+ executeScript ( scrollToTopPosition ) ;
5867 } }
5968 onClickScrollBottom = { ( ) => {
60- executeScript ( scrollToBottomScript ) ;
69+ executeScript ( scrollToBottomPosition ) ;
6170 } }
6271 />
6372 </ div >
0 commit comments