Skip to content

Instantly share code, notes, and snippets.

@lexoyo
Last active January 27, 2019 02:31
Show Gist options
  • Save lexoyo/a10b1d9ef934bd0a184f177e341c40ff to your computer and use it in GitHub Desktop.
Save lexoyo/a10b1d9ef934bd0a184f177e341c40ff to your computer and use it in GitHub Desktop.
Page rank

Page rank user script

About this user script

It adds a button to google search result, then ask you for a website and browse all result pages to find the position of this website in google.

screenshot from 2019-01-26 20-19-14

TODO:

  • use the URL un get rather than localstorage => enable multiple tabs
  • mark the result in the list rather than alert => looks nice, do not bug in firefox
  • check the page even if not "searching" => when you search google, your websites have their page rank beside them
// ==UserScript==
// @name Page rank
// @namespace http://tampermonkey.net/
// @version 0.1
// @description add a button to google search result, then ask you for a website and browse all result pages to find the position of this website in google
// @author You
// @match https://www.google.com/search*
// @grant none
// ==/UserScript==
(function() {
'use strict';
const INTERVAL_WAIT = 10000;
const defaultUrl = localStorage.getItem('page-rank-url') || '';
if(location.search
.split("&")
.filter(param => param === 'page-rank-user-script')
.length > 0) {
searchFor(defaultUrl);
const btnStr = `
<button id='page-rank-stop'>Stop search</button>
`;
document.querySelector('#hdtb-msb div').insertAdjacentHTML('beforeend', btnStr);
document.querySelector('#page-rank-stop').onclick = () => stopPageRank();
}
else {
const btnStr = `
<button id='page-rank-start'>Start search</button>
`;
document.querySelector('#hdtb-msb div').insertAdjacentHTML('beforeend', btnStr);
document.querySelector('#page-rank-start').onclick = () => startPageRank(defaultUrl);
}
function stopPageRank(defaultUrl) {
location.search = location.search.replace('&page-rank-user-script', '');
}
function startPageRank(defaultUrl) {
const url = prompt('which URL', defaultUrl);
if(url) {
localStorage.setItem('page-rank-url', url);
location.search += '&page-rank-user-script';
}
}
function searchFor(url) {
const result = checkPage(url);
if(result.length === 0) {
console.log('continue');
nextPage();
}
else {
stopPageRank();
const rank = document.querySelector('.cur').innerText;
alert('Found ' + result.join(', ') + ' at page ' + rank);
}
}
function checkPage(url) {
return Array.from(document.querySelectorAll('cite'))
.map(el => el.innerHTML)
.filter(u => u.indexOf(url) >= 0)
}
function nextPage() {
const arr = Array.from(document.querySelectorAll('.pn'));
const a = arr[arr.length - 1];
a.search += '&page-rank-user-script';
console.log(a);
setTimeout(() => doClick(a), Math.random() * INTERVAL_WAIT);
}
function dispatchMouseEvent(target, var_args) {
var e = document.createEvent("MouseEvents");
// If you need clientX, clientY, etc., you can call
// initMouseEvent instead of initEvent
e.initEvent.apply(e, Array.prototype.slice.call(arguments, 1));
target.dispatchEvent(e);
}
function doClick(element) {
console.log('do click', element);
dispatchMouseEvent(element, 'mouseover', true, true);
dispatchMouseEvent(element, 'mousedown', true, true);
dispatchMouseEvent(element, 'click', true, true);
dispatchMouseEvent(element, 'mouseup', true, true);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment