cielavenir/ctouch

Undisposition for chrome: toggle behaviour (active/not active)

Opened this issue ยท 6 comments

I really like this extension, but I feel the need from time to time to disable it, and going through Chrome->Extension and so on is uncomfortable.

I wrote a few lines for toggling the extension with a click on the button, feel free to add them to your extension if you like.

undisposition_bg.js become

var active = true;
chrome.webRequest.onHeadersReceived.addListener(
	function(details){
	//console.log(details);
		var headers=details.responseHeaders;
		if (active)
		{
			for(var i=0;i<headers.length;i++){
				if(headers[i].name.toLowerCase()=='content-disposition'){
					if(headers[i].value.indexOf('attachment')==0){
						headers.splice(i,1);
						//headers[i].value='inline';
					}
					break;
				}
			}
			for(var j=0;j<headers.length;j++){
				if(headers[j].name.toLowerCase()=='content-type'){
					if(headers[j].value=='application/octet-stream'||headers[j].value=='application/x-download'){
						headers[j].value='text/plain'; //I hope Chrome is wise enough
					}
					break;
				}
			}
		}
		return {responseHeaders: headers};
	},
	{
		urls: ['<all_urls>'],
		types: ['main_frame','sub_frame']//,'stylesheet','script','image','object','xmlhttprequest','other']
	},
	['blocking', 'responseHeaders']
);

function loadOptions(callback)
{
	chrome.storage.local.get('activeStatus', function(data) {
		if (data.activeStatus === undefined) //at first install
		{
			data.activeStatus = true;
			saveOptions();
		}
		active = data.activeStatus;
		if (callback != null)
			callback();
	});
}

function saveOptions()
{
	chrome.storage.local.set({ activeStatus: active });
}

function updateUI()
{
	console.log("updateUI end, active = " + active);

	var str = active? "Undisposition active, click to deactivate": "Undisposition disabled, click to activate";
	chrome.browserAction.setTitle({title:str});
	chrome.browserAction.setBadgeText({text:active?"Act":"Dis"});
}

function ToggleActive()
{
	active = !active;
	saveOptions();
	updateUI();
}

loadOptions(updateUI);
chrome.browserAction.onClicked.addListener(ToggleActive);

in manifest.json, add browser_action and the permission for storage (in order to remember last status set)
e.g.

{
  "background": {
      "scripts": [ "undisposition_bg.js" ]
   },
   "description": "Remove Content-Disposition: attachment HTTP header.",
   "key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDLBkZmL28J6RP1R8G5XutDFiaZyZqfvrmam8R6DMCrEeE7ncgF4r5tAWXMa7U/ulrfBV8IP9zvnNAt6smAVlugxBHTmUyYL1B7TUMaH2/WGNx0tlQWRMbNv3sWKcrhR/N1MdBCAfzkaSZ98NqPDCdoituwCkK275gLou8hTtG2UwIDAQAB",
   "manifest_version": 2,
   "name": "Undisposition",
   "permissions": [ "webRequest", "webRequestBlocking", "\u003Call_urls>", "storage" ],
   "update_url": "http://clients2.google.com/service/update2/crx",
   "version": "0.0.1",
   "browser_action": { } 
}

Right now I'm running my local (modified) copy of your extension, if you publish a version with the above feature I will surely install it.

I'm very noob with JS, if you find something orrible.... please let me know!

Thanks for your little and so precious extension
Gian Paolo

That's great, installed. Thanks @Racle

Racle commented

I'll share this in same thread. Some love for Firefox users also https://addons.mozilla.org/en-US/firefox/addon/undisposition-racle-fork/

@Racle Is your fork open source?

Racle commented

@Racle Is your fork open source?

I haven't released that to GitHub. It's basically code above with small tweaks so I just quickly modified and uploaded to Chrome/Firefox store.

Got no problem to get that on GitHub also (I'll do this when I'm at PC next time). You can also probably just unpack extension from official source and look that for your self. It's in plain text and has not been minimized :)

Racle commented

@andyneff full source can be found here: https://github.com/Racle/undisposition