how qclean works? introduction to browser extensions

36
How QCLean works? Introduction to Browser Extensions @qcl Code & Beer 2015.03.06

Upload: qing-cheng-li

Post on 06-Aug-2015

36 views

Category:

Software


1 download

TRANSCRIPT

Page 1: How QCLean Works? Introduction to Browser Extensions

How QCLean works?Introduction to Browser Extensions

@qclCode & Beer 2015.03.06

Page 2: How QCLean Works? Introduction to Browser Extensions
Page 3: How QCLean Works? Introduction to Browser Extensions

What is QCLean?

● A browser extension/add-on that removing ads, suggested pages and posts on Facebook news feed for Chrome, Firefox, Safari, Opera and IE users.

● http://qcl.github.io/QCLean/

Page 4: How QCLean Works? Introduction to Browser Extensions

History

● Since 2013.02.28 (2 years old!)● Popular add-on (Firefox) 2013.04.06● Popular extension (Chrome) 2013.08.21

Page 5: How QCLean Works? Introduction to Browser Extensions

Overview

● Support 5 browsers○ Chrome/Chromium, Firefox, Safari, Opera, IE

● More than 30,000 users over the world.○ GC ~30,000○ FF ~8,500

● Donated mainly by Firefox user!○ Receive about $24○ Donate NOW!

Page 6: How QCLean Works? Introduction to Browser Extensions

Overview

● Rating○ ★★★★★ 4.5/5.0 Chrome○ ★★★★ 4.0/5.0 Firefox○ ★★★★ 4.0/5.0 Opera

● Operating System○ 93% Windows

● Languages○ Chrome 74% zh-tw○ Firefox 53% en-us

Page 7: How QCLean Works? Introduction to Browser Extensions

About icon

Page 8: How QCLean Works? Introduction to Browser Extensions

Extension/Add-on v.s. Plugin

● Extension/Add-on○ Extensions are small software programs that can

modify and enhance the functionality of the browser. You write them using web technologies such as HTML, JavaScript, and CSS. (Chrome)

● Plugin○ Plugins are shared libraries that users can install to

display content that the application itself can't display natively. (MDN)

Page 9: How QCLean Works? Introduction to Browser Extensions

Extensions

● Written by HTML/JavaScript/CSS● chrome://extensions● about://addons

Page 10: How QCLean Works? Introduction to Browser Extensions

Plugins

● Written by C/C++, depend on platform○ NPAPI, NaCl (Chrome)

● chrome://plugins● about://plugins

Page 11: How QCLean Works? Introduction to Browser Extensions

Plugins

● NPAPI ○ PCManX GTK2 on Chrome using NPAPI

@qcl, @JeromeWu, 2010

Page 12: How QCLean Works? Introduction to Browser Extensions

Extension developing 101

● Firefox○ http://blog.qcl.tw/2013/08/sdkfirefox-add-on.html

● Chrome○ Create a file named manifest.json○ Open your Chrome, go to chrome://extensions○ Click Developer Mode○ Then import the folder which contains manifest.json

Page 13: How QCLean Works? Introduction to Browser Extensions

manifest.json

{“manifest_version” : 2,“name” : “extension_name”,“version” : “version_string” ,“description” : “extension_description”...

}

● https://developer.chrome.com/extensions/manifest

Page 14: How QCLean Works? Introduction to Browser Extensions

manifest.json (QCLean)

Page 15: How QCLean Works? Introduction to Browser Extensions

Background

background: {“script”:[“background.js”]

}

● It exists for the lifetime of your extension.● Only ONE instance of it at a time.● It can be used to manage some task or state.

Page 16: How QCLean Works? Introduction to Browser Extensions

Permissions

● permissions: [“tabs”]

● https://developer.chrome.com/extensions/declare_permissions● https://developer.chrome.com/extensions/permission_warnings

Page 17: How QCLean Works? Introduction to Browser Extensions

Content Scripts

“content_scripts” : [{

“matches”: [match patterns],“css”: [“kerker.css”],“js”: [“haha.js”],“run_at”: “document_start”

}]

● https://developer.chrome.com/extensions/content_scripts

Page 18: How QCLean Works? Introduction to Browser Extensions

Content Scripts

● Content scripts are JS that run in the content of web page

● Can not use chrome.* API of extension, i18n, runtime, storage

● Can not use functions defined by extension page

● Can not use functions defined by web page or other content script

● Match Pattern○ https://developer.chrome.com/extensions/match_patterns

Page 19: How QCLean Works? Introduction to Browser Extensions

Content Script

● run_at○ doucment_start

■ Before any other DOM is constructed or any other script is run

○ doucment_end■ After DOM is complete, but before subresources

like images and frames have loaded○ doucment_idle (defualt)

■ After window.onload

Page 20: How QCLean Works? Introduction to Browser Extensions

Remove Suggested Pages&Posts

● First commit of QCLean

Page 21: How QCLean Works? Introduction to Browser Extensions

Remove Suggested Pages&Posts

● What will happen when all posts on your newsfeed use the same class name?○ All the posts will be removed :p

● Or the class name used by other elements on the page○ Those elements will be removed, too :p

● Or you can not get Ad elements directly

● Select elements that you can identify then try to do something.

Page 22: How QCLean Works? Introduction to Browser Extensions

Remove Suggested Pages&Posts

● Select some special elements, try to find the Ad block, then remove it.

● If Facebook changes the structure of page, this method will GG

● It will run once when page did load. (run_at)

Page 23: How QCLean Works? Introduction to Browser Extensions

Facebook infinite scrolling

● When to run the script?○ document_start? document_end? document_idle?○ setInterval?

● When you scrolls down the page, it will load more and more post (and Ads)

● Need to find a trigger point to remove Ads!

Page 24: How QCLean Works? Introduction to Browser Extensions

Override the prototype of DOM

● Check Ads when appending new elements into the page.

● Override the prototype of some DOM○ HTMLDivElement.prototype.appendChild○ HTMLUlElement.prototype.appendChild

Page 25: How QCLean Works? Introduction to Browser Extensions

Prevent Requesting Ads

● Check AJAX requests by overriding XMLHttpRequest.prototype

Page 26: How QCLean Works? Introduction to Browser Extensions

Content Scripts

● Can not override the prototype of HTML DOM in web page (directly)

● But we can add new element into web page.

Page 27: How QCLean Works? Introduction to Browser Extensions

Override prototype is DANGEROUS

Page 28: How QCLean Works? Introduction to Browser Extensions

MutationObserver

● QCLean-Chrome-Experiment

https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver

Page 29: How QCLean Works? Introduction to Browser Extensions

ClassName changed frequently

● Static classnames list is not enough● I want to build a API for user, it can update

classnames list.

Page 30: How QCLean Works? Introduction to Browser Extensions

Some QCLean API

● Story API○ http://qcl.github.io/QCLean/api/story.js

● Line Tagging API○ http://qcl.github.io/QCLean/api/lineTagging.js

● XHR Cross-origin request issue● Content security policy issue

Page 31: How QCLean Works? Introduction to Browser Extensions

Content Security Policy

“content_security_policy” : “policy”

● It can not be show on permission warning● CSP works as a black/whitelisting mechanism

for resources loaded or executed by your extension.

https://developer.chrome.com/extensions/contentSecurityPolicyhttp://www.html5rocks.com/en/tutorials/security/content-security-policy/

Page 32: How QCLean Works? Introduction to Browser Extensions

Distribute Extensions

Cost Review Publish

Chrome $5 no directly publish

Firefox free optional directly publish

Opera free required after review

Safari free required unknow*

Page 33: How QCLean Works? Introduction to Browser Extensions

Extension source

● Web Store / Addon Center○ You don’t need to mantain a update server

● Mantain your own update server

Page 34: How QCLean Works? Introduction to Browser Extensions

Update URL

update_url : “http://qcl.github.io/QCLean/chrome_update.xml”

● Packaging your extension (with key)○ extension.crx

● Write update XML○ https://developer.chrome.com/extensions/autoupdate

Page 35: How QCLean Works? Introduction to Browser Extensions

How others write extensions?

● Install the extension● Get the extension’s ID (chrome://extensions)● Go to

○ ~/Library/Application Support/Google/Chrome/Default/Extensions (mac)

Page 36: How QCLean Works? Introduction to Browser Extensions

QCLeanhttps://github.com/qcl/QCLean