Switch to Google : How I did It

Last office week of Ramadan, at office I was waiting for Azure Remote app to import the disk image. It will take about 30 mins while everyone went for lunch. Yes, just a minute before I have published my third Google chrome extension, Switch to Google while I was alone with the PC. It employs chrome.pageAction API to put the icon on addressbar for a partucular URL.

screenshot

Sometimes this doesn’t make sense, why I have to use this? but for me, my default search engine is Bing for no reason and I find good search results there also, for some instances I have to come back to Google, and I was using a small trick there. Bing searches with http://www.bing.com/search?q=this+is+a+search while Google also follows the same thing https://www.google.com/search?q=this+is+a+search so I manually replace bing to google and today it took me 2 minutes to develop this one.

More technically, the manifest file goes like

{
  "name": "Switch to Google",
  "version": "1.0",
  "description": "Tired of being in Bing, on click solution to switch to Google is here",
  "background": {
    "scripts": ["background.js"],
    "persistent": false
  },
  "page_action" :
  {
    "default_icon" : "icon-19.png",
    "default_title" : "This is Bing"
  },
  "permissions" : [
    "declarativeContent"
  ],
  "icons" : {
    "48" : "icon-48.png",
    "128" : "icon-128.png"
  },
  "author": {
  "name": "Isham Mohamed .MI",
  "email": "ishammohamed@outlook.com",
  "url": "http://ishammohamed.me"
  },
  "manifest_version": 2
}

This points to a background.js file which looks like

// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// When the extension is installed or upgraded ...
chrome.runtime.onInstalled.addListener(function() {
  // Replace all rules ...
  chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
    // With a new rule ...
    chrome.declarativeContent.onPageChanged.addRules([
      {
        // That fires when a page's URL contains a 'g' ...
        conditions: [
          new chrome.declarativeContent.PageStateMatcher({
            pageUrl: { urlContains: 'bing.com/search' },
          })
        ],
        // And shows the extension's page action.
        actions: [ new chrome.declarativeContent.ShowPageAction() ]
      }
    ]);
  });
});

function checkForValidUrl(tabId, changeInfo, tab) {
  if (tab.url.indexOf('.bing.com') > -1) 
    chrome.pageAction.show(tabId);
};

chrome.tabs.onUpdated.addListener(checkForValidUrl);

chrome.pageAction.onClicked.addListener(function(tab){
  var currentURL = tab.url.replace("bing","google");
    chrome.tabs.update(tab.id, {url: currentURL});
});

The code is pretty much clear and I am doing the same replace thing with the help of this JS.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s