Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome Extension, scrollTop=0 enywhere

I'm making a chrome extension. And I've some problems with scrollTop (I hope this is the "get scroll height"!).

First: structure: I've a manifest, an html page, a javascript, and an image.

Second: Aim. I'm trying to scroll down to bottom, than go to top, and restart to scroll to bottom.. a loop. In manifest I've this:

{
 "background": {
       "page": "scroll.html"
 },
 "browser_action": {
       "default_icon": "icon.png",
       "default_title": ""
 },
 "content_scripts": [ {
       "js": [ "scroll.js" ],
       "matches": [ "http://*/*", "https://*/*" ]
 } ],
 "description": "Auto Scroll. UpDownUp!",
 "icons": {
       "128": "icon.png",
       "48": "icon.png"
 },    
 "manifest_version": 2,    
 "name": "Auto Scroll, UpDownUp",    
 "permissions": [ "tabs", "http://*/*", "https://*/*" ],    
 "version": "0.2.3"
}

As you see I don't have any pop up html.

In my html page i've this:

<html>
<head>
<title>Auto Scroll</title>
</head>
<body >
<script src="scroll.js"></script>
</body> 
</html>

And In my scroll.js there are several functions, to start scrolling, and scrolling faster. But I don't need to quote all that code (164 lines).

I've simply a lot of trash, and at the end it does only this simple thing:

chrome.tabs.update(id, {'url': 'javascript:document.body.scrollTop+=1;'});

My extension "works". But I've not done all what I wanted:

This extension will only go down (and it does!!). I want that when I'm at bottom of page, the extension will execute:

chrome.tabs.update(id, {'url': 'javascript:document.body.scrollTop=0;'});

And I thought:

1) get max scroll height, check with scrolltop, if same, use that code. If i do so, the extension will always go on top (after a little scroll down), as if these values are always(!!) equals.

if(document.body.scollTop==document.body.Height)
{
chrome.tabs.update(id, {'url': 'javascript:document.body.scrollTop=0;'});
}

this is the result of first try... but doesn't works.

2) var y=document.body.scrollTop --> scroll --> z=document.body.scrollTop. If y==z, then I'm on bottom, then go to top... this doesn't works, y and z are always equals!!!

I used an alert(value) every time I use

chrome.tabs.update(id, {'url': 'javascript:document.body.scrollTop+=1;'});

set as

alert(document.body.scollTop);

and pop up appear with "0". Always... I close the pop up, the extension scroll down a bit more, and pop up appear and there is 0 again!

Ok... I'ts my fault!?

I've an idea: my javascript is loaded by a background page, and the javascript act on a page in browser, mybe the scrollTop command give me background page height... and this is really BAD. Can you help me finding the way to stop at end of "active" page to execute the "go-to-top" function!?

Edit:

I found something, if I use this

chrome.tabs.getSelected(null, function(tab) { 
    alert(tab.url);
})

a window pop us telling me the acqual tab url. Looking at this I find that height is the tab properties, so I use:

    chrome.tabs.getSelected(null, function(tab) { 
    alert(tab.height);
})

but the window popup and say "undefined" .... I really don't know what I've to do to retrieve the actual tab scroll position.

like image 628
Raikoug Avatar asked Jan 25 '26 18:01

Raikoug


1 Answers

Solved, finally. I managed to do a all in 2 functions:

function scrolla(){
    chrome.tabs.executeScript({code: 'var x= document.body.scrollTop; document.body.scrollTop+=1; var y=document.body.scrollTop; if(x==y){document.body.scrollTop=0;}'});
}

and

chrome.browserAction.onClicked.addListener(function(tab) {

      var start = setInterval(function(){scrolla()},10);

    });

This will scroll the page untill the end, than restart from top. I manged to do this very very hardly! HAHHA Solved!!!!

like image 170
Raikoug Avatar answered Jan 27 '26 07:01

Raikoug



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!