Skip to content

Instantly share code, notes, and snippets.

@atropnikov
Last active February 9, 2017 11:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atropnikov/b38ae64bac5f9b5e33232ea840884e21 to your computer and use it in GitHub Desktop.
Save atropnikov/b38ae64bac5f9b5e33232ea840884e21 to your computer and use it in GitHub Desktop.
Adguard Extension Api
// Global API object
var adguardApi = {
	/**
	 * Starts application. Loads filters metadata, rules and creates request filter.
     * If application was configured before, it starts with previous configuration.
	 */
    start: function(callback) {
    },
    /**
	 * Stops application (Unloads rules from memory)
     */
    stop: function(callback) {
    },
	/**
	 * Updates and applies configuration. 
	 * If you want to configure filters, you should at least once invoke start method.
     * Method can throw error in following cases:
     * 1. Application isn't initialized (i.e. 'start' method was never called before)
     * 2. filters array contains wrong filter identifier
	 */
    configure: function(configuration, callback){
    },
	/**
	 * Registers or removes event listener for request blocked event
	 */
	onRequestBlocked: {
		/**
		 * Registers event listener
		 * Fired when a request is blocked
         *  adguardApi.onRequestBlocked.addListener(function(details){...});
         *  details = {
         *      tabId: ..., // Tab identifier
         *      requestUrl: "...", // Request URL
         *      referrerUrl: "...", // Referrer URL
         *      requestType: "...", // "SUBDOCUMENT","SCRIPT","STYLESHEET","OBJECT","IMAGE","FONT","XMLHTTPREQUEST","OTHER"
         *      rule: "..." // Rule text
         *   };
		 */
		addListener: function(callback){},
		/**
		 * Removes event listener
		 */
		removeListener: function(callback){}
	}
};

configuration = {
    
    /*
     * Array of filters identifiers. Overrides current enabled filters.
     * If filters is undefined it has no effect.
     */
    filters: [],

    /**
     * Array of domains. 
     * If it is defined, api works in default mode: block ads everywhere except the whitelist.
     */
    whitelist: [],
    
    /**
     * Array of domains.
     * If it is defined, api works in inverted mode: unblock ads everywhere except the blacklist.
     * If both whitelist and blacklist are defined blacklist is used. If neither whitelist nor blacklist are defined it has no effect.
     */
    blacklist: [],
    
    /** 
     * Custom filters URLs
     * filtersMetadataUrl URL for fetching filters metadata.
     * filterRulesUrl URL for fetching filte rules. It must contain {filter_id} substitution.
     */
    filtersMetadataUrl: 'https://.../metadata.json',
    filterRulesUrl: 'https://.../{filter_id}.txt'
};
// Add event listener
adguardApi.onRequestBlocked.addListener(function (details) {
	console.log(details);
});
// Configures URLs
adguardApi.configure({
    filtersMetadataUrl: 'https://somedomain.com/metadata.json',
    filterRulesUrl: 'https://somedomain.com/{filter_id}.txt'
});
// Start application
adguardApi.start(function () {
    console.info('Application was started');
});
...
var configuration = {
    filters: [1, 2], // Use Russian and English filters
    whitelist: ['example.com', 'about.com'] // Do not block ads on example.com and about.com
};
adguardApi.configure(configuration, function () {
    console.info('Configuration was updated and applied');
});
...
adguardApi.stop(function () {
    console.info('Application was stopped');
});

List of available filters can be obtained from https://filters.adtidy.org/extension/chromium/filters.json Look at filters section

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment