XFoxPrower / Amazon Autopager

// ==UserScript==
// @name			Amazon Autopager
// @version			1.07
// @author			XFox Prower
// @namespace		http://www.TailsArchive.net/
// @description		Loads next page of search results automatically on Amazon
// @include			https://www.amazon.com/s/*
// @include			https://www.amazon.com/gp/search/*
// @include			https://smile.amazon.com/s/*
// @include			https://www.amazon.ca/s/*
// @include			https://www.amazon.de/s/*
// @include			https://www.amazon.co.uk/s/*
// @include			https://www.amazon.co.jp/s/*
// @grant			none
// ==/UserScript==

'use strict';

function fscroll()
	{
	var
		next,
		req,
		nexthref;

	if(lock)
		{
		return;
		}
	next=D.getElementById('pagnNextLink');
	if(next.getBoundingClientRect().bottom<innerHeight>>1)
		{
		lock=true;
		cont=D.getElementById('s-results-list-atf');
		pager=D.getElementById('pagn');
		nexthref=next.href;
		req=new XMLHttpRequest();
		req.open('GET',nexthref);
		req.responseType='document';
		req.onload=xhrLoaded;
		req.send();
		history.pushState(null,'',nexthref);
		}
	}

function xhrLoaded()
	{
	const
		res=this.response,
		col=res.querySelectorAll('li[id^=result_]'),
		len=col.length,
		frag=D.createDocumentFragment();
	var
		i=0;

	while(i<len)
		{
		frag.appendChild(col[i]);
		i++;
		}
	cont.appendChild(frag);
	pager.parentNode.replaceChild(res.getElementById('pagn'),pager);
	lock=false;
	}

const
	D=document;
var
	cont,
	pager,
	lock=false;

addEventListener('scroll',fscroll);