Firefox extension to get google pagerank and alexa ranking


Question: 

I am creating a Mozilla Firefox toolbar to show PageRank and Alexa ranking of current website. One way I came to know is to use XMLHttpRequest in my JavaScript file to get information from a PHP page hosted on my website's server.

The PHP class has this function:

function check($page) {

// Open a socket to the toolbarqueries address, used by Google Toolbar
$socket = fsockopen("toolbarqueries.google.com", 80, $errno, $errstr, 30);

// If a connection can be established
if($socket) {
  // Prep socket headers
  $out = "GET /tbr?client=navclient-auto&ch=".$this->checkHash($this->createHash($page)).
          "&features=Rank&q=info:".$page."&num=100&filter=0 HTTP/1.1\r\n";
  $out .= "Host: toolbarqueries.google.com\r\n";
  $out .= "User-Agent: Mozilla/4.0 (compatible; GoogleToolbar 2.0.114-big; Windows XP 5.1)\r\n";
  $out .= "Connection: Close\r\n\r\n";

  // Write settings to the socket
  fwrite($socket, $out);

  // When a response is received...
  $result = "";
  while(!feof($socket)) {
    $data = fgets($socket, 128);
    $pos = strpos($data, "Rank_");
    if($pos !== false){
      $pagerank = substr($data, $pos + 9);
      $result += $pagerank;
    }
  }
  // Close the connection
  fclose($socket);

  // Return the rank!
  return $result;
}

Is there a better way to get page ranks in my custom Firefox toolbar without having to host a PHP service?

 // Create a url hash
function createHash($string) {
$check1 = $this->stringToNumber($string, 0x1505, 0x21);
  $check2 = $this->stringToNumber($string, 0, 0x1003F);

$factor = 4;
$halfFactor = $factor/2;

  $check1 >>= $halfFactor;
  $check1 = (($check1 >> $factor) & 0x3FFFFC0 ) | ($check1 & 0x3F);
  $check1 = (($check1 >> $factor) & 0x3FFC00 ) | ($check1 & 0x3FF);
  $check1 = (($check1 >> $factor) & 0x3C000 ) | ($check1 & 0x3FFF);  

  $calc1 = (((($check1 & 0x3C0) << $factor) | ($check1 & 0x3C)) << $halfFactor ) | ($check2 & 0xF0F );
  $calc2 = (((($check1 & 0xFFFFC000) << $factor) | ($check1 & 0x3C00)) << 0xA) | ($check2 & 0xF0F0000 );

  return ($calc1 | $calc2);

}

// Create checksum for hash

function checkHash($hashNumber)
{
  $check = 0;
$flag = 0;

$hashString = sprintf('%u', $hashNumber) ;
$length = strlen($hashString);

for ($i = $length - 1;  $i >= 0;  $i --) {
  $r = $hashString{$i};
  if(1 === ($flag % 2)) {
    $r += $r;
    $r = (int)($r / 10) + ($r % 10);
  }
  $check += $r;
  $flag ++;
}

$check %= 10;
if(0 !== $check) {
  $check = 10 - $check;
  if(1 === ($flag % 2) ) {
    if(1 === ($check % 2)) {
      $check += 9;
    }
    $check >>= 1;
  }
}

return '7'.$check.$hashString;

}




1 Answer: 

If your PHP code only makes an HTTP request then you can just do the same request from the Firefox extension as well:

var request = new XMLHttpHeader();
request.open("http://toolbarqueries.google.com/tbr?client=navclient-auto&ch=...");
request.setRequestHeader("User-Agent", "Mozilla/4.0 (compatible; GoogleToolbar 2.0.114-big; Windows XP 5.1)");
request.send();

However, you should clarify whether this use of a Google server (particularly masquerading as Google Toolbar) complies with their Terms of Service. Otherwise you might find yourself confronted with legal action or at the very least sudden changes in the way this web service works.

As to the hash function: obviously you can either translate this algorithm to JavaScript (which is pretty straightforward from the look of it) or search around to see whether anybody did it already. E.g. I found this JS-based hash algorithm implementation (it's a different algorithm that is prefixed with 8 instead of 7 however, note also that this prefix isn't returned by the hash function but is rather part of the URL there).

 

More Articles


node.js - Microsoft Botframework Prompts.choice not getting validation response

I am using chatconnector to connect my Bot to an frontend chat app and writing the bot's response to my own database. The problem is when I am validating a prompt the bot responds but there's no API for me to grab the validation response. bot.dialog('/', [function (session) { builder.Prompts.cho

node.js - Firebase calling `child(all).equalTo(id)`

I like to check all children that contains a child with a value to the variable id.This is what I came up with:usersref.child(formData.openid + '/votes').child().equalTo(formData.appid).once('value', function(snapshot) {});This returned: Firebase.child failed: Was called with 0 arguments. Expects at

node.js - Get data having a maximum attribute from firebase in nodejs

So i am working with firebase in nodejs, there is a "number" attribute in each of my document of a specific table(name generated at runtime). I want to get the data having the attribute "number"'s maximum value.Here is my sample data:- -L1GIb7Vyn6Yhd5gghH0 correct: blah number: 9 questio


node.js - Hand Off Bot to Human using BotFramework (NodeJS)

I have crated a server application using botframework in NodeJS and configured for web chat channel in botframework.I want to validate the every reply that bot delivers to end user and those messages will be validated by human from an external web application.How can we trigger the bot from external

jquery - How do I access a child named with a random id in firebase?

I want to be able to automatically access every child named with random ID keys nested inside "posts", is there a way to get the children of every child inside them?I am completely new to firebase and managing database in general (started learning code 3 months ago), so I might have missed it in the

I came across the new Stack Overflow Bot by Microsoft on the Stack Overflow blog and decided to give it a try.After installing Node V8.1.4 for macOS, I installed the bot by running npm install in the StackBot directory. There weren't many issues, except for a warning returned.MacBook-Air:StackBot US


node.js - BotBuilder TypeError: builder.ConsoleConnector is not a constructor

I'm following the official quick start of Microsoft Bot Builder(SDK v3) for NodeJs: Create a bot with the Bot Builder SDK for Node.js 1- I made a new project with npm init2- then npm install --save botbuilder3- then I created a new file "app.js"var builder = require('botbuilder');var connector =

Logs showing WARNING when trying to credit bank accounts with Balanced Payments

Been seeing this in my logs today. ############################################################# # WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! # ############################################################# Using positional arguments is **DEPRECATED**. Please use the keyword optio

node.js - Correct way to send response when using Actions-on-google with koa.js

I'm trying to use actions-on-google with koa framework. I can't understand which request and response to use with AoG constructor in koa middleware. This is my code:const Koa = require( 'koa' )const koaBody = require('koa-body')const { DialogflowApp } = require('actions-on-google')const koaApp = new

actions on google - Implementing Conversation Webhook as Azure Function App

I have an azure function app that I am using as a webhook for my google assistant action. I've tried to follow the documentation for the proper response but I continually get the following error in the simulator when testing my webhook. Is there anything in my response message that looks wrong?Fai