How to Make Your First Chrome Extension

I’ve made a couple of Chrome extensions and talked about them here before. With the release of XKCD 1625 last week, I thought it would be a good opportunity to describe the process of creating an extension and publishing it in the Chrome Web Store. You can find the example extension created in this post on the Web Store. Commit messages in the accompanying Github repo provide additional details about each step. You can find more information on creating Chrome extensions in this article from Google.

1. Write the functionality that your extension will provide

This can be anything that’s allowed by the Chrome program policies. In the example extension, there’s a function that iterates over a series of keys used to generated regular expressions and replaces them with the more fun suggestions in the XKCD comic above.

var dictionary = {
    "debate":                               "dance-off",
    "self\\-?driving":                      "uncontrollably swerving",
    "candidate":                            "airbender",
    ...
}

for (key in dictionary) {
    content = content.replace(new RegExp(key, "g"), dictionary[key]);
  ...
}

return content;

2. Write a background file with instructions for the browser on how to execute your script

For this part you will interact with the Chrome browser’s APIs. The purpose of this file is to link browser actions to the functionality you developed in the previous step. In the example app, when the user clicks the app icon in their browser toolbar, a new tab will be opened using the current URL with the XKCD replacements applied to the document’s markup.

chrome.browserAction.onClicked.addListener(function(tab) {
  chrome.tabs.duplicate(tab.id, function(tab){
    chrome.tabs.executeScript(null, {file: "xkcdsub.js"});
  });
});

3. Write a manifest file with info for the Chrome Web Store

Here’s where you put information that will help users find your extension and understand what it does.

Some notes about this file:

  • The manifest_version has to be “2” for current versions of Chrome.
  • Limit the permissions you need as much as possible (in the example app, it needs the user’s current page URL for either http or https connections, and the ability to execute a script on the user’s active tab).
  • Which script(s) to execute in the background (background.js in our case).
  • Icon images for the extension (19×19, 38×38, and 128×128-pixel varieties, the last of which can also be used for the extension’s icon in the web store).

4. Test your extension locally

You’ll want to test out your extension before uploading it, to ensure that it behaves as expected. You can load the extension from your local filesystem by navigating to chrome://extensions in the browser and clicking “Load Unpacked Extension”. There will be a dialog informing you if any errors occurred (such as an incorrect manifest version). If there are no errors, the extension will be loaded and accessible from the Chrome toolbar by clicking the icon you set up in the previous step. If you make any changes to your code locally, you’ll need to reload the extension.

5. Set up a Chrome Developer account

To publish your app in the web store you will need a Chrome Developer account and accompanying web page. Because you will have to verify that you can access the web page in order to associate it with your extension, it’s easiest to use a blogspot.com account if you are new to this process.

6. Publish your extension to the Chrome Web Store

Once you’re satisfied with your app’s behavior and ready to release it to the world, you can publish it to the web store. In this step you will supply additional details about your app to users and provide one or more screenshots or Youtube videos illustrating its functionality. Here’s the example extension applied to this news story:

xkcdsub1625-example.png

7. … Profit!?!

It is quite rewarding to know that you can deliver code to enhance and extend the functionality of a web browser for users. Users can leave feedback on your extension in the web store, so check back occasionally to see if there are opportunities to improve it. Feel free to leave any feedback about this post or the accompanying extension here or open an issue on Github. If you use these tips to make your own Chrome extension I would love to hear about it!

What’s the Worst That Can Happen?

What’s the worst that can happen if you make a five-year plan? That everything goes according to plan. While this seems counter-intuitive, thinking about what it means for your future to look exactly like you expect it to can shed some light on the values and dangers of planning.

If your life or business is exactly “on track” in a few years with what you expect it to be today, that means at least one of the following is true. Either you gained no new information during that time, which means you aren’t learning, or you took no significant risks, which means you aren’t trying. You may say, “I played it safe and stuck to the plan,” but that only means that you minimized downside risk–and therefore minimized upside risk as well.

I have been thinking about this recently based on Ben Thompson’s comments in Exponent #38 (45:41):

My favorite way to think about life and career is [that] the problem with having a
five-year or ten your plan is that you succeed. You look up and realize you’re way off from where you should be because the world has changed under your feet. You were so dedicated to following through on something that you miss what’s happening.

These thoughts were elaborated on more eloquently in the most recent episode, Exponent #53 (29:39):

If you think about the future and always make plans for the future, the worst thing about having a super-explicit five year plan is that you’ll achieve your plan perfectly and then you get there and you realize that the world has changed while you’ve been focused on your plan. You realize that, yeah, you got to the destination you wanted to get to but it’s nowhere near the place you’re supposed to be. There’s so much about the world that we don’t control–basically everything–and the only thing I can control is me. And the only thing I can control about me is my actions and responses to whatever is happening right now.

The comments are in the context of education and career decisions, but are applicable more broadly. Both episodes–and the whole podcast, really–are well worth your time.

What Can Software Developers Learn from Tigers?

Object-oriented programming is a powerful way of modeling the world. Objects encapsulate data and behavior, and can interact and be composed in many useful ways.

As developers, one question we often consider is which types (in both technical and non-technical sense of the word) of objects to privilege by building them into our systems. Is an Address a proper object, or is it just a bit of data that can be encapsulated under a User? Are PowerUsers and NewUsers different enough to merit their own classes? (Probably not.)

But as our systems evolve, it can become difficult for existing object models to respond to new requirements. That’s apparently what is currently happening with tiger species. Because there are nine recognized tiger species, preservation efforts are spread across the six non-extinct species. A new proposal based on examination of DNA suggests that there should only be two tiger species.

As one researcher quoted in the piece says, “It’s really hard to distinguish between tigers…. The taxonomies are based on data from almost a hundred years ago.” Although your object model may not have legacy code that is quite that old, this case demonstrates the importance of reconsidering what traits and behaviors you allow to be first-class citizens in your system.

In the case of tigers, reducing the number of recognized species to two (one inhabiting continental Asia, and another the archipelago of Indonesia) would allow conservationists to try more flexible strategies. One example mentioned in the article is moving tigers within the same (redefined) species from one area to another (the updated definition of a continental Asian tiger would include the Amur tiger of Russia, the Bengal of India, and the South-China tiger). To a non-expert, it seems that interbreeding between these population groups would also help increase their numbers and perhaps increase genetic diversity.

Figuring out how to classify real-life entities can be very difficult. For tigers, what characteristics define a species? A century ago it might have been their physical appearance, while today we can look at the genetic level. In software, we have to think hard about the taxonomies we choose because they quickly become metaphors we live by.

The lesson for developers is that making your object model too fine-grained can introduce unexpected constraints when requirements change. To paraphrase Keynes, “When my information changes, I alter my code.”

Aviation, Suicide, and the Germanwings Crash

Note: If you are struggling with thoughts of depression or suicide, please refer to the resources at the end of the post.

Four weeks ago tomorrow, a Germanwings A320 crashed in the French Alps resulting in the deaths of all 150 individuals on board. I will not recount the details here, but investigators who were able to reconstruct the events leading up to the crash found evidence from cockpit records that the first officer intentionally crashed the plane. This post highlights three issues related to the crash: the extreme rarity of all airline disasters, let alone those caused by deliberate pilot action; risks associated with the Werther effect; and government and airline regulations regarding pilots’ mental health issues.

From 1976 to 2013, there were only eight commercial aircraft accidents attributed to pilot suicide by the Aviation Safety Network. Out of millions of pilots, over one billion commercial flights, and many billions of passengers (counting repeats, obviously) that is a vanishingly small rate. An FAA review of aircraft-assisted pilot suicides in the US identified eight such events between 2003 and 2012, al limited to general aviation (i.e. non-commercial). This is a decrease by half from the previous review covering the decade before. Not only is pilot suicide incredibly rare, it is also becoming less frequent.

If this crash makes you more likely to drive instead of fly, you will be putting yourself at much greater risk. When airline traffic decreased by about 15-20 percent following the events of September 11, 2001, Gerd Gigerenzer estimated that fatal car crashes went up by about 10 percent (due to excess road miles driven), accounting for about 350 additional deaths.

In addition to a disproportionate fear response, widespread reporting of suicides can also inspire copycat behavior. This phenomenon is known as the Werther effect, named for the title character in Wolfgang von Goethe’s novel The Sorrows of Young Werther which was blamed for a rash of suicides amongst young males across Europe in the 1770s. This copycat behavior was popularized in Robert Cialdini’s book Influence and based on earlier research by David Phillips. In a 1980 paper, Phillips found that

After publicized murder—suicide stories there is an increase in noncommercial plane crashes and an increase in airline crashes. This increase in crashes persists for approximately nine days, and then the level of crashes returns to normal.

The paper is gated, but some figures from it can be found here. Evidence for the Werther effect implies that the media should be very careful with how they present stories of suicide. This piece by Matt Haig does a very good job of discussing the Germanwings incident and can be summarized by its title and opening blurb:

I’ve suffered from depression. The only life I’ve ever considered taking is my own. For 15 years I was depressive on and off, and the ignorant speculation about Andreas Lubitz’s mental health strikes me as being harmful.

Although the ultimate responsibility for safety lies with pilots, airlines and government agencies also have a responsibility to passengers to ensure that potentially suicidal pilots receive treatment and do not put others at risk. In the US, the FAA takes this duty very seriously. As one of my flight instructors said, “The FAA doesn’t care about you, they care about the people you might endanger.” In the 2003 report mentioned above, the authors note:

All pilots involved in these aircraft-assisted suicides were male, with a median age of 46 years. The pilot was the sole occupant in 7 of the 8 aircraft that were intentionally crashed. Four of the 8 pilots were positive for ethanol, and 2 of the 8 were positive for Selective Serotonin Reuptake Inhibitor (SSRI) antidepressants.

Unfortunately demographics of professional pilots overlap considerably with demographics most likely to commit suicide.

Current FAA aeromedical regulations require that individuals on one of four approved SSRIs undergo additional screening before before receiving their aviation medical certificate. Pilots with a diagnosed mental illness must also see an approved medical examiner every six months for a neurological screener. Airlines also typically require an annual MMPI screening for pilots. (Note that these regulations do not apply in Europe, and I am unfamiliar with the process there.) While this process is admirable in its emphasis on passenger safety, the increased burden on pilots is enough to persuade some to conceal their illness from the FAA, or worse, forego formal treatment altogether (self-medicating with other substances is all too common). The aviation industry has a long way to go in making mental health issues more open for discussion, as does much of society.

What can be done to preserve pilot and passenger safety? If you are a potential passenger, do not switch to driving when you hear about airline accidents. If you comment on these issues on social media, avoid glamorizing suicide. If you are a pilot or in the aviation community, be aware of FAA regulations, do not fly if you are in a mentally unsound state, and be willingly to openly discuss these issues. Finally, if you are experiencing mental health issues reach out to someone around you.

In the US:

  • Mental Health America has community support groups around the country. Their toll free number is (800) 969-6642.
  • Mental Health First Aid offers instruction in responding to mental health crises.
  • The FAA aeromedical office can be reached at (405) 954-4821.
  • For software developers, Open Sourcing Mental Illness is an effort by Ed Finkler to encourage wider discussion of mental health issues in our community. It is supported by EngineYard’s Prompt campaign. Ed has some great videos here.

In the UK (from the BBC article above):

  • Rethink Mental Illness has more than 200 mental health services and 150 support groups across England. Its number is 0300 5000 927.
  • Samaritans provides 24-hour emotional support for people who are experiencing feelings of distress or thoughts of suicide. Its number is 08457 90 90 90.
  • Papyrus works with young people to prevent suicide. Its helpline number is 0800 068 4141

Internationally:

  • You can find a list of suicide prevention hotlines by country here.

Edward Snowden on Privacy, Values, and Government Surveillance

John Oliver’s recent interview with Edward Snowden is circulating widely online, but given the ignorance displayed in “man on the street” segments it deserves more attention. Those segments are included in the video below, and misapprehensions range from the belief that Snowden sold secrets to associating him with WikiLeaks (that’s not Snowden, it’s Julian Assange).

To be clear, this is not a sympathetic interview. There are some humorous parts, but Oliver is not tossing softballs. He asks Snowden difficult questions about whether he understood the implications of the information he released, and whether turning it over to journalists was a responsible decision. As a Snowden supporter some parts of the conversation were frustrating to me, but that probably means that it was a fair interview.

The most important part of the video below concerns Section 215 of the Patriot Act, which allows government collection of “any tangible thing” that is “relevant to” a terrorism investigation. That’s an unbelievably broad provision, and one that its author James Sensenbrenner claims has been misinterpreted by lenient FISA courts and should be put out of business. We must have a very serious national conversation about this bill before its renewal on June 1 of this year.

There were several good quotes from Edward Snowden in the interview. Here are my two favorites:

You will never be completely free from risk if you’re free.

You shouldn’t change your behavior because a government agency somewhere is doing the wrong thing. If we sacrifice our values because we’re afraid, we don’t care about those values very much.

The “Manual for Civilization” Project: A Library for the End of the World

With the subtitle, “How to Rebuild Our World from Scratch,” you can probably guess the genre of The Knowledge. I read this ambitious book over the holidays, hoping that I could learn some of the basics of fields I’m less familiar with such as organic chemistry and medicine. On that front the book delivers, but does it live up to its title?

Some parts of the book were very practical while others seemed superfluous. Purifying water with bleach (p. 37) could be useful in even a small-scale disruption. But in the wake of a larger disaster I find it hard to believe that knowing how to build an internal combustion engine (p. 199) or mix gunpowder (p. 232) would be near-term priorities. (As an aside, the book contains a one-decimeter line segment from which you can reconstruct the entire metric system, but I happen to think that less formal systems of measurement such as the acre–the amount of land a yoke of oxen could plow in a day–would become popular in apocalyptic scenarios.)

The Knowledge is a fun read and contains some useful tips, but I would not want it to be my go-to book for emergencies. That is why I was interested to learn of the “Manual for Civilization” initiative, started by The Long Now Foundation.  This is a library of books that were listed by domain experts and Long Now staff and donors in answer to the question “If you were stranded on an island (or small hostile planetoid), what books would YOU want to have with you?

After reading through the answers I have compiled a short list of my own with the additional qualification that the book offers knowledge that is beneficial even if disaster doesn’t strike. The name after the title is the first recommender on whose list I noticed the book, with a link to their full list of recommendations. (Kevin Kelly’s compilation seemed especially good; his book Cool Tools would likely fit in the list below).

The Future of Imagination

Venkatesh Rao had a great piece last week on imagination as a survival skill. Here is the gist:

I suspect failure-to-self-actualize will become the leading cause of death (or madness) in the developed world.

Rao defines “self-actualization” as

the imaginative embodiment of internal realities (what the daemon feels) in the form of a dent in the universe: a surprising and free external reality that actualizes a new possibility for all

and “imagination” as

the ability to create unpredictable new meaning while generating more freedom than you consume.

The post is very good, worth reading twice. However, there is one key shift that Rao overlooks. He focuses on imagination as an essential ability for the wealthy (the “one percent”), but it has even bigger implications for a future of 100 percent unemployment.

This change is coming, slowly but surely. It’s hard to imagine a future where “the robots take over” entirely, but we are already seeing a society where the poorest have more leisure time than the wealthiest. As Arnold Kling writes:

The prediction I would make is that we would see a lot more leisure. For those whose skill adaptation is adequate, that leisure will take the form of earlier retirement, later entry into the work force, or shorter hours. For those whose skill adaptation is inadequate, that leisure will show up as unemployment or reluctant withdrawal from the labor force.

I think that if you look only at males in isolation, you will see this in the data. That is, men are working much less than they used to. For some men, this leisure is very welcome, but for others it is not. In that sense, I think that we should look at the fears of the early 1960s not as quaint errors but instead as fairly well borne out.

The availability of inexpensive leisure (think cable TV and Youtube) has increased the reservation wage of low-wage workers. This has made unskilled individuals less willing to work for near-minimum wage jobs, as detailed in this New York Times article.

Self-actualization of highly skilled individuals as described by Rao has created so much freedom for those at the bottom of the income distribution that they now choose not to work. In their own words, though, the willingly unemployed do not seem to live fulfilling lives. Self-actualization is as important for them as it is for the wealthy, but they suffer from a failure of imagination.

Design Patterns for Cooking

Last week Alexey introduced the idea of cooking patterns:

A recipe is basically a fixed set of actions and ingredients, while cooking techniques are just the possible actions. If we invent cooking patterns – an abstraction on top of each ingredient / action pair – we could have more understanding of the dish we are preparing while keeping the flexibility in ingredient and technique choice.

Let’s take fritters as an example. Wikipedia says the following:

Fritter is a name applied to a wide variety of fried foods, usually consisting of a portion of batter or breading which has been filled with bits of meat, seafood, fruit, or other ingredients.

A pattern in its most obvious form. Notice the “wide variety”, a fixed ingredient (batter) and a list of possible variables (meat, seafood, vegetables, fruit) that could influence the fritters you end up making.

I find this idea very exciting, because I enjoy cooking and am also in the process of learning more about software design patterns.

Cooking patterns seem like an accessible way to introduce beginners to more abstract ideas about software, too. Algorithms are often described as “recipes,” and this is a nice way to build on that concept.

For leveling up your cooking skills, ChefSteps looks promising. Their resources include classes, projects, and an ingredients wiki. I have signed up for one class and plan to follow up on this recommendation after completing it.

If you are interested in cooking patterns, check out the Github repo or read the full article.

A Checklist for Using Open Source Software in Production

A great majority of the web is built on open source software. Approximately two-thirds of public servers on the internet run a *nix operating system, and over half of those are Linux. The most popular server-side programming languages also tend to be open source (including my favorite, Ruby). This post is about adding a new open source library to an existing code base. What questions should you ask before adding such a dependency to a production application?

The first set of questions are the most basic. A “no” to any of these should prompt you to look elsewhere.
  • Is the project written in a language you support? Is it in a language you support? If not, is it compatible (e.g. through stdin/stdout or by compiling to your language of choice)?
  • Is the project in a version of of the language you support? If it’s written in Python 3 and you only support Python 2, for example, using this library could lead to headaches.
  • Can you use the project in your framework of choice (e.g. Rails or Django)?
  • Are there conflicts with other libraries or packages you’re currently using? (This is probably the hardest question to answer, and you might not know until you try it.)
Assuming there are no immediate technical barriers, the next questions to ask are of the legal variety. Open source licenses come in many flavors. In the absence of a license, traditional copyright rules apply. Be especially careful if the project you are investigating uses the GPL license–even basing the code you write off of a GPL open source project can have serious legal ramifications. There’s a great guide to OSS licenses on Github. If you’re the author or maintainer of an open source project checkout choosealicense.com.
The next thing to consider is whether and how the project is tested. If there is not an automated test suite, consider starting one as your first contribution to the project and be very reluctant to add the project to your application. Other related questions include:
  • Are there unit tests?
  • Are there integration tests?
  • What is the test coverage like?
  • Do the tests run quickly?
  • Are the tests clearly written?
Finally, by using an open source project you are also joining a community of developers. None of these questions are necessarily show-stoppers but knowing the size of the community and the tone of its discourse can save you pain down the road.
  • Is the project actively maintained? When was the last commit?
  • Does the community have a civil, professional style of debate and discussion?
  • Is there only one developer/maintainer who knows everything? This doesn’t have to be a deal breaker. However, if there is a single gatekeeper you should make sure you understand the basics of the code and could fork the project if necessary.

This is by no means an exhaustive list but these questions can serve as a useful checklist before adding an open source as a dependency for your project.

A New Wiki for Computer Science Symbols

Computer science is increasingly relevant to a wide range of professional fields, yet many working programmers today do not have a formal CS education. This makes it difficult for the uninitiated to read academic research in computer science and related fields. Keeping up with the latest research is not a job requirement for most programmers, but understanding fundamental papers (such as the ones listed on Papers We Love) is important for building on established knowledge.

However, jargon and unfamiliar symbols present a non-trivial barrier to entry. This came up in the discussion on a recent episode of the Turing Incomplete podcast. A few existing resources were mentioned such as Wikipedia’s math symbols page and Volume I of The Art of Computer Programming. None of these is ideal for new programmers who may not know the names of the symbols, though.

That’s why I started a CS notation wiki. There are currently four pages, one each for computational symbols, linguistic symbols, logical symbols, and mathematical operators. Each page currently only has a few entries, but requests for additional ones can be filed as Github issues. New contributions are certainly welcome, and should be submitted as pull requests. Contribution guidelines can be found on the wiki’s home page. Other suggestions can be submitted as comments here, via email, or on Twitter. Let me know how this could be more useful to you!