Posts tagged Chrome

Friday Fun: Build Your First Chrome Extension

Google Chrome is the most popular Web browser in the world. Part of its appeal comes from its ability to let you fully customize your browsing experience with a slew of extensions. Extensions are small, lightweight programs that personalize your Chrome installation with new features.

You’ve probably already downloaded an extension or two. But did you know it’s almost as easy to build your own? Chrome extensions are written in a relatively beginner-friendly language—JavaScript—and require only two files to function.

Since they’re so easy to build, there are currently more than 53,000 extensions in the Chrome Web Store, ranging from productivity tools to stupid entertainments.

Today’s Project

Today we’re going to build a Chrome extension that isn’t particularly useful, though it’s sort of funny. We’ll be transforming Steven Frank’s Cloud to Butt Plus extension, which edits every Web page you visit by replacing the phrase “the cloud” with “my butt.” You can judge the results for yourself.

With Frank’s permission, we’ll be creating a derivative work out of his Cloud to Butt GitHub repository in order to build a “find and replace” extension of our own.

Since I rarely build a coding project that isn’t trolling my coworkers in some way, my example envisions the Web the way my Paleo editor Owen Thomas probably sees it. The Paleo diet puts carbohydrates off limits, so I decided to make his dietary choices simple by making “bread,” “pasta,” and related taboo foodstuffs less appealing to him on the Web.

My finished extension is on GitHub for anyone who wants to use it. Here’s how it works.

Anatomy Of An Extension

Extensions piggy-back off of existing Chrome functionality to add new features. This means anyone can build an extension using HTML, CSS, and JavaScript, without having to learn to work with Chrome’s native code. As the Chrome Developer site promises:

If you’ve ever built a web page, you should feel right at home with extensions pretty quickly.

That’s quite an assertion, and it certainly depends on the complexity of the extension you want to build. Still, all you need for a basic extension are these:

1. A manifest.json file. Here, .json stands for JavaScript Object Notation. This manifest file stores metadata about our extension and shows Chrome how to use it. Every manifest file includes the extension’s name and description for Chrome Web Store browsers. After that, it declares dependencies, permissions, and any browser actions the extension will perform.

2. A JavaScript or HTML file. Here’s where you write the program detailing what your extension does. In the example the Chrome Developer site gives, it’s popup.html, a page that delivers cute cat photos to extension-users. For more complex extensions, it’s a JavaScript file containing a program that delivers the meat of the extension.

3. An icon. Actually, this is optional, but it’s helpful and certainly looks cute when your extension is installed. For best results, save an icon as three square images at resolutions of 16px, 28px, and 128px.

Building Manifest.json

At its very minimum, a manifest file needs only to include a name and a version. At 17 lines, ours does a little more. (Here’s the full thing in one place.)

This part includes all the metadata:

{
  "manifest_version": 2,
  "name": "Caaaarbs",
  "version": "1.0",
  "description": "Paleo's best friend.",
  "icons": {
    "16": "images/carbs16.png",
    "48": "images/carbs48.png",
    "128": "images/carbs128.png"
    },

Manifest_version refers to the version of the file format we’re using. Chrome requires that you use version 2, so that’s what we’ve indicated.

Next comes the extension name, version, and description. These are really up to you.

After that, I listed out the extension’s icon sizes. First, I picked an image that I thought fit my extension—a royalty-free vector graphic of a croissant—and then sized it in Photoshop three times. Now, Chrome automatically puts the correct size of the icon where it is needed.

Here’s the rest of the file:

  "content_scripts":
  [
    {
      "matches": ["*://*/*"],
      "js": ["myscript.js"],
      "run_at": "document_end"
    }
  ]
}

These are the content scripts that make the extension tick. The first one here simply indicates that my extension will do its thing on any website. Under different circumstances, you could edit the asterisk wildcards to limit use of the extension to particular pages—you know, like http://readwrite.com.

The second line indicates that manifest.json will read in the extension’s underlying program from a JavaScript file named myscript.js. That’s where the whole “find and replace” function lives.

Finally, the third line instructs my extension to run after the full page has loaded in the browser window. If it ran before I brought up a site, some of the words I want to find and replace might not have loaded yet!

Building Myscript.js

This file may be 40 lines long (see it here), but it’s mainly home to two JavaScript functions. In programming, a function is a reusable bit of code that performs a specific task.

The first function, called walk, executes an action that JavaScript programmers refer to as “walking the DOM.” DOM stands for Document Object Model, which is a code-based representation of a Web page and every element—text, images, form fields, and so forth—on it. It sort of resembles an upside-down tree, with a single trunk at the top and a bunch of ordered code “branches” below.

The walk function explores the whole tree, starting at the trunk and moving down to the end of the first branch, then back up until it finds another branch to examine. Basically, it’s crawling all the data on the page to locate the textual elements.

That’s where the second function, handleText, comes in. When walk finds some text, handleText scans for the words we want to replace, and then replaces them wherever it finds them.

How does it know which words to replace? We specified that this way:

v = v.replace(/\bbread\b/g, "caaaaarbs");

This is one of the five lines that specifies the words I want to swap out. You can choose any number of words for substitution, though each one will need a line like the one above. (It’s not the most graceful program ever written, but it is straightforward.)

Some technical details, for those who are interested: “v” is a variable that stores a temporary copy of “textNode.nodeValue=”—i.e., the text in a particular text element called “textNode.” The function v.replace rewrites the text in that element by replacing the first string (everything inside the parentheses before the comma) with the second string (the word “caaaaarbs”). The first string in the example above is a dense bit of code that identifies all text matching “bread” and then instructs the function to replace every word that is a match to this one.

At the end of the function, the temporary value stored in “v” gets copied back to “textNode.nodeValue” and then written into the code representation of the Web page—which then displays your change in the browser.

Uploading to Chrome

Collect your manifest.json, myscript.jpg, and your icons in a new folder by themselves. Now, navigate to chrome://extensions/ in your browser window.

Now, click the checkbox to put Extensions in “Developer Mode.” This will give you a few more options regarding what you can do with your extensions.

Click “Load unpacked extension…” and navigate to your Chrome extension folder. If all is well, it should upload without a hitch. If it returns an error, there’s most likely a syntax error in your code, so check it and try again.

Success! The image above shows my extension among others I’ve installed.

Now check out all the code to my extension on GitHub, clone your own copy, and make your own find-and-replace extension. I used this to prank my editor, but the possibilities are endless! You could prank your family or friends, too. Or you could even—gasp—use the find-and-replace action to create something useful! 

In any case, I’d love to see what you build. Tell us all about it in comments.

Engineer Jack Lawson contributed to this article. 

Top photo by Darren Harve; all screenshots by Lauren Orsini

View full post on ReadWrite

SearchCap: Google CPC Declines, Microsoft Cortana & Bing In Chrome

Below is what happened in search today, as reported on Search Engine Land and from other places across the web. From Search Engine Land: Microsoft Bing Now Added To Google Chrome New Tab Page Microsoft’s search engine, Bing, is now an option within Google’s Chrome new tab page screens….



Please visit Search Engine Land for the full article.

View full post on Search Engine Land: News & Info About SEO, PPC, SEM, Search Engines & Search Marketing

Microsoft Bing Now Added To Google Chrome New Tab Page

Microsoft’s search engine, Bing, is now an option within Google’s Chrome new tab page screens. Prior, if you changed your default search provider in Chrome, the new tab page would still show Google has the search option. It would change the search provider in the URL/omnibox area but it…



Please visit Search Engine Land for the full article.

View full post on Search Engine Land: News & Info About SEO, PPC, SEM, Search Engines & Search Marketing

Google Now Comes To Chrome For Desktops & Laptops

Google’s amazing predictive search tool, Google Now, is finally being made available to people on desktop and laptop computers using the regular release of its Chrome browser. Until now, it had only been available through mobile devices or to those using beta versions of Chrome. Google shared…



Please visit Search Engine Land for the full article.

View full post on Search Engine Land: News & Info About SEO, PPC, SEM, Search Engines & Search Marketing

Google Now is Coming to Desktop Computers, via Chrome

Personal assistant Google Now, which monitors your web searches and alerts you about things such as the weather, flights, and traffic as well as providing various event reminders, has previously been available only on Android devices.

View full post on Search Engine Watch – Latest

Google Now Cards Hit The Desktop For Mac, Windows and Chrome OS Users

The ever-helpful Google Now service is now available on your desktop for both Mac, Windows and Chrome OS users. This Google Now service features Google Now cards that are run through the latest Google Chrome build (Chrome Canary) and will work for those users who are currently logged-in to Chrome….



Please visit Search Engine Land for the full article.

View full post on Search Engine Land: News & Info About SEO, PPC, SEM, Search Engines & Search Marketing

Google Chrome Can Cut Your Mobile Data Usage—But There’s A Catch

The latest Chrome for Mobile update could help lower your data usage, but there’s a caveat—it will pass everything you browse through Google servers. 

Google announced new Chrome data compression and bandwidth management that it says could cut your browsing data usage in half. This is an opt-in feature that you can turn on via the browser’s settings.

The data compression feature is powered by an SPDY proxy connection running on Google’s servers that reduces the latency of Web pages. For instance, when you use Chrome, the proxy is able to process multiple requests and responses over a single connection to your mobile device, and the proxy takes on the task of DNS lookups that would normally be handled by your phone or tablet. The proxy also optimizes images to a compressed WebP format, so less data is used when downloading images. 

There’s more background in this older Google Chrome blog post.

Chrome will bypass the Google proxy (and thus disable data compression) if you’re on a secure (HTTPS) connection or browsing in “incognito” mode. Google takes some further steps to mollify privacy concerns:

Additionally, the content of proxied pages will be cached according to each page’s cache policy as specified by its headers (Expires, Cache-Control, etc.) but not logged. These logs are not associated with your Google Account, and the entire log entry will be removed within 6 months. Google will use both the request and response data to improve the service; for example, more effective optimizations can be uncovered by analyzing timing data for pages loaded through the proxy service.

You’ll be able to track how much bandwidth you save each month by visiting “Bandwidth Management” under “Settings.” 

In addition to the data saving update, Google also announced Google Translate in Chrome for iOS and application shortcuts on Android. 

Image via Google

View full post on ReadWrite

Middle Earth Comes To Life On Google Chrome

Editor’s note: This post was originally published by our partners at PopSugar Tech.

From the elvish oasis of Rivendell to the abandoned kingdom of Erebor, director Peter Jackson spared no expense in re-creating J.R.R. Tolkien’s fantastical world of lush greenery, terrifying forest lands, and creatures big and small. In The Hobbit: The Desolation of Smaug, the trifecta of stunning backdrops, flawless costumes, and sweeping music comes together seamlessly on the big screen (and in eye-popping 3D).

Now imagine the difficulty in developing that same universe for small screens. A Journey to Middle-earth is a Google Chrome experiment that took the challenge head-on. The interactive Web feature for computers, phones, and tablets allows Hobbit fans to see the dwarves’ journey in incredible detail.

Explore the Trollshaw Forest, Rivendell, Dol Guldur, Thranduil’s Hall, Lake-Town, and Erebor in full-screen. You’ll definitely want to pop on some over-ear headphones and experience the Chrome Experiment’s immersive soundtrack. The narration provided in each area will provide background from Tolkien’s novel and the first Peter Jackson-directed Hobbit film. Once you finish swiping through each area, you’ll get a chance to play an interactive game. Try your hand at archery in Lake Town or avoid the wrath of Smaug by delivering only the cleverest response.

Creating the web-based multimedia was no small feat. Engineers used WebGL and Web Audio to produce the interactive’s rich detail and optimize javascript performance. Adapting the experience for all screens was another challenge entirely. The team provided a case study of how they approached the front-end development for the site.

As fans of both Tolkien and Sherlock, we are very, very excited to see The Hobbit: The Desolation of Smaug again and again. The film, of course, stars our two favorites: Martin Freeman as Bilbo and Benedict Cumberbatch as the lethal Smaug. The banter between the two of them in the film is priceless.

After watching the film, dive into the Hobbit Chrome Experience at middle-earth.thehobbit.com, then let us know which section of the dwarves’ adventure you’d like to participate in most. 

Image courtesy of Flickr user torbakhopper via CC

More stories from PopSugar Tech:
7 All-Female Coding Camps That Are Changing the Ratio
Shh! 3 Creative Uses For Pinterest’s “Secret Boards”
Best of 2013: The Top 5 Science Tweets of the Year
YouTube Breaks Down the Top Geek Videos of the Year
Regeneration Hour: Watch the “Time of the Doctor” Trailer

View full post on ReadWrite

Go to Top
Copyright © 1992-2015, DC2NET All rights reserved