Permalink
Browse files

adding in the extension. just using google search filter as a basis.

  • Loading branch information...
1 parent a4cfb26 commit b4313e7ae595d1611809469af65bf59496f610c2 @paulirish committed Jan 14, 2011
@@ -0,0 +1,241 @@
+
+<div id="log">
+</div>
+
+
+<script type="text/javascript" src="jquery-1.4.2.js"></script>
+
+<script>
+var synced = false;
+
+function set_sync_name(name)
+{
+ window.localStorage.setItem("sync_name", name);
+};
+
+function get_setup_complete()
+{
+ if (window.localStorage.getItem("setup_complete"))
+ {
+ return true;
+ }
+ return false;
+};
+
+function set_setup_complete(value)
+{
+ value = value || true
+ window.localStorage.setItem("setup_complete", value);
+};
+
+function get_sync_name()
+{
+ var sync_name = window.localStorage.getItem("sync_name");
+ if (sync_name && sync_name.length > 0)
+ {
+ return sync_name;
+ }
+ return false;
+};
+
+function has_synced(value)
+{
+ return synced;
+};
+
+function set_synced(value)
+{
+ if (value)
+ {
+ synced = value;
+ return;
+ }
+};
+
+function update_filters(newline_separated, callback)
+{
+ var urls = newline_separated.split("\n");
+ var new_urls = new Array();
+ for (i = 0; i < urls.length; i++)
+ {
+ var temp_url = urls[i];
+ temp_url = $.trim(temp_url);
+ if (temp_url.length > 0)
+ {
+ new_urls.push(temp_url);
+ }
+ }
+ window.localStorage.setItem("filter_urls", JSON.stringify(new_urls));
+ update_filters_online(callback);
+};
+
+function get_sync_url()
+{
+ return 'http://clobapi.heroku.com/clob/' + encodeURI(get_sync_name());
+};
+
+function get_filters_online(callback, force)
+{
+ if (!get_sync_name())
+ {
+ get_filters();
+ if (callback)
+ {
+ callback();
+ }
+ return;
+ }
+ if (has_synced() && !force)
+ {
+ if (callback)
+ {
+ callback();
+ }
+ return;
+ }
+ $.ajax({
+ url: get_sync_url(),
+ success: function(data) {
+ update_filters(data);
+ synced = true;
+ if (callback)
+ {
+ callback();
+ }
+ },
+ error: function() {
+ update_filters("");
+ update_filters_online(function () {
+ synced = true;
+ if (callback)
+ {
+ callback();
+ }
+ });
+ }
+ });
+};
+
+function update_filters_online(callback)
+{
+ if (!get_sync_name())
+ {
+ if (callback)
+ {
+ callback();
+ }
+ return false;
+ }
+ $.ajax({
+ type: 'PUT',
+ url: get_sync_url(),
+ data: { body: get_filters_raw() },
+ success: callback
+ });
+};
+
+function get_filters()
+{
+ if (window.localStorage.getItem("filter_urls"))
+ {
+ return JSON.parse(window.localStorage.getItem("filter_urls"));
+ }
+ else
+ {
+ var array = new Array();
+ return array;
+ }
+};
+
+function get_filters_raw()
+{
+ return get_filters().join("\n");
+};
+
+function should_filter(url)
+{
+ var filters = get_filters();
+ var action = null;
+ var current_filter = null;
+ for (i = 0; i < filters.length; i++)
+ {
+ current_filter = filters[i].toLowerCase();
+ action = 'hide'; // default action
+ if (current_filter.indexOf('#') === 0)
+ {
+ continue;
+ }
+ if (current_filter.indexOf('+') === 0)
+ {
+ action = 'amplify';
+ current_filter = $.trim(current_filter.slice(1));
+ }
+ if (url.toLowerCase().indexOf(current_filter) != -1)
+ {
+ return action;
+ }
+ }
+ return false;
+};
+
+
+function on_request(request, sender, sendResponse)
+{
+ var response = {}
+ if (request.should_filter)
+ {
+ //switch (should_filter(request.should_filter))
+ switch ('notanything')
+ {
+ case 'hide':
+ response.action = 'hide';
+ break;
+ case 'amplify':
+ response.action = 'amplify';
+ break;
+ case false:
+ // do nothing
+ break;
+ }
+ }
+ sendResponse(response); // snub them.
+};
+
+function on_tab_updated(tab_id, change_info, tab)
+{
+ if (tab.url.indexOf('www.google.') != -1 && tab.url.indexOf('www.google.com/reader') == -1 && tab.url.indexOf('www.google.com/calendar') == -1)
+ {
+ chrome.pageAction.show(tab_id);
+ }
+ else
+ {
+ chrome.pageAction.hide(tab_id);
+ }
+};
+
+//chrome.extension.onRequest.addListener(on_request);
+chrome.extension.onConnect.addListener(function(port) {
+ port.onMessage.addListener(function(request) {
+ var response = {}
+ if (request.should_filter)
+ {
+ switch (should_filter(request.should_filter))
+ {
+ case 'hide':
+ response.action = 'hide';
+ break;
+ case 'amplify':
+ response.action = 'amplify';
+ break;
+ case false:
+ // do nothing
+ break;
+ }
+ }
+ response.href = request.should_filter;
+ port.postMessage(response);
+ });
+});
+
+chrome.tabs.onUpdated.addListener(on_tab_updated);
+</script>
Binary file not shown.
@@ -0,0 +1,5 @@
+.amplified_result
+{
+ padding: 5px;
+ background-color: #C8FFDE; /* very light #E8FFE9 */
+}
@@ -0,0 +1,40 @@
+function check_links()
+{
+ $('a.l').each(function () {
+ port.postMessage({ should_filter: $(this).attr('href') });
+ });
+};
+
+function stop_timer()
+{
+ clearInterval(check_links_timer);
+}
+
+var port = chrome.extension.connect({name: "knockknock"});
+port.onMessage.addListener(function(response) {
+ var result_a = $('a.l[href="' + response.href.replace('"', '\\"') + '"]');
+ switch (response.action)
+ {
+ case 'hide':
+ result_a.parent().parent().parent().hide();
+ break;
+ case 'amplify':
+ if (!result_a.parent().parent().parent().hasClass('amplified_result'))
+ {
+ result_a.parent().parent().parent().addClass('amplified_result');
+ }
+ break;
+ default:
+ result_a.parent().parent().parent().show();
+ result_a.parent().parent().parent().removeClass('amplified_result');
+ break;
+ }
+});
+
+if (window.location.host.indexOf('www.google.') != -1 && window.location.pathname.indexOf('/reader') == -1 && window.location.pathname.indexOf('/calendar') == -1)
+{
+ var check_links_timer = setInterval(check_links, 750);
+}
+
+
+
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.

0 comments on commit b4313e7

Please sign in to comment.