Browser Extension, Firefox, Javascript, Share-It

Firefox Extension – Share-it 0.6 Context Menu

Share-it provides a context menu that allows the user to share the current page with a number of different websites that allow for link submissions. Share-it currently shares a web page on: facebook, twitter, google plus, linkedin, digg, stumbleupon, tumblr, friendfeed, fark, blinklist, and plurk. There is an options page that will allow the user to turn on or off the menu items they choose to use. Very simple and minimalistic design.

Share-it Context Menu Opened Up In Firefox

Share it Options Dialog Opened Up In Firefox

package.json

{
  "name": "share-it",
  "fullName": "Share-it",
  "id": "jid1-Kl2M45Ix2rhGtA",
  "description": "Adds context menu items to share the current page on a number of websites ",
  "author": "Timothy Tocci - timothytocci.com",
  "homepage": "https://timothytocci.com/",
  "license": "MPL 2.0",
  "version": "0.5"
}

main.js


var cm = require("sdk/context-menu");
var data = require("sdk/self").data;
var tabs = require("sdk/tabs");
var SS = require("simple-storage");

popupPanel = require("panel").Panel({
        height: 500,
        contentURL: data.url("options.html")
    });
popupPanel.port.on("optionsstarted", function () {
	console.log("optionsstarted received");
    popupPanel.port.emit("entrylist", entries);
});
popupPanel.port.on("setActive", function (menu) {
    setActive(menu);
});
popupPanel.port.on("setInactive", function (menu) {
    setInactive(menu);
});
var defaultentries = [
	{
	"menu":"facebook.com",
	"active":1,
	"ustr":"http://www.facebook.com/sharer.php?u={{URL}}&src={{TITLE}}"
	},{
	"menu":"twitter.com",
	"active":1,
	"ustr":"https://twitter.com/share?url={{URL}}&text={{TITLE}}"
	},{
	"menu":"plus.google.com",
	"active":1,
	"ustr":"https://plusone.google.com/_/+1/confirm?hl=en&url={{URL}}"
	},{
	"menu":"linkedin.com",
	"active":1,
	"ustr":"http://www.linkedin.com/cws/share?url={{URL}}"
	},{
	"menu":"digg.com",
	"active":1,
	"ustr":"http://digg.com/submit?url={{URL}}&title={{TITLE}}"
	},{
	"menu":"stumbleupon.com",
	"active":1,
	"ustr":"http://www.stumbleupon.com/submit?url={{URL}}"
	},{
	"menu":"tumblr.com",
	"active":1,
	"ustr":"http://www.tumblr.com/share?v=3&u={{URL}}&t={{TITLE}}"
	},{
	"menu":"friendfeed.com",
	"active":1,
	"ustr":"http://www.friendfeed.com/share?url={{URL}}&title={{TITLE}}"
	},{
	"menu":"fark.com",
	"active":1,
	"ustr":"http://cgi.fark.com/cgi/fark/farkit.pl?u={{URL}}&h={{TITLE}}"
	},{
	"menu":"blinklist.com",
	"active":1,
	"ustr":"http://www.blinklist.com/index.php?Action=Blink/Addblink.php&Url={{URL}}&Title={{TITLE}}"
	},{
	"menu":"plurk.com",
	"active":1,
	"ustr":"http://plurk.com/?qualifier=shares&status={{TITLE}}%20%2D%20{{URL}}"
	},
];
var entries = {};
if (!SS.storage["entries"]) {
	SS.storage["entries"] = JSON.stringify(defaultentries);
}
entries = JSON.parse(SS.storage["entries"]);
function setActive(menu){
	console.log("setActive");
	var elen = entries.length;
	console.log(menu);
	for(i=0;i<elen;i++){
		if(entries[i]["menu"] == menu){
			entries[i]["active"] = 1;
		}
	}
	SS.storage["entries"] = JSON.stringify(entries);
}
function setInactive(menu){
	console.log("setInactive");
	var elen = entries.length;
	console.log(menu);
	for(i=0;i<elen;i++){
		if(entries[i]["menu"] == menu){
			entries[i]["active"] = 0;
		}
	}
	SS.storage["entries"] = JSON.stringify(entries);
}
var mitems = [];
var entlen = entries.length;
for (i=0;i<entlen;i++){ 	if(entries[i]["active"] == 1){ 		mitems.push(cm.Item({ label: entries[i]["menu"], data: entries[i]["ustr"] })); 	} } mitems.push(cm.Separator()); mitems.push(cm.Item({ label: "Options", data: "{{OPTIONS}}" })); cm.Menu({   label: "Share-it",   contentScriptFile: data.url("menuscript.js"),   items: mitems,   onMessage: function(data) {sendit(data);} }); function sendit(data){ 	if(data.lastIndexOf("{{OPTIONS}}")!==-1){ 		popupPanel.show(); 	}else{ 		var url = data; 		url = url.replace("{{URL}}", encodeURI(tabs.activeTab.url)); 		if(data.lastIndexOf("{{TITLE}}")>0){
			url = url.replace("{{TITLE}}", encodeURIComponent(tabs.activeTab.title));
		}
		tabs.open({
			url: url,
			onReady: function onReady(tab) {
				console.log(tab.title);
			}
		});
	}
}

options.html


<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Share-it Options</title>
</head>
<style>
.green{color:green;}
.red{color:red;}
li{list-style-type: none;}
</style>
<body>
<h3>Share-it Context Menu Options</h3>
<ul id="menu">
</ul>
<div class="summary">Changes will take place the next time you start your browser. 
Or if you don't want to close the browser you could disable and reenable the Extension on the Plug-ins page.</div>
<p>Click anywhere outside this window to close it.</p>
</body>
<script type="text/javascript">
window.onload = function () {
	addon.port.emit("optionsstarted");
	var list = document.getElementById("menu");
	addon.port.on("entrylist", function (ckb) {
		for(i = 0; i < ckb.length; i++){
			if(ckb[i]["active"] == 1){
				var li = document.createElement("li");
				cbox = document.createElement("input");
				cbox.type = "checkbox";
				cbox.setAttribute("class","butt");
				cbox.checked = true;
				cbox.value = ckb[i]["menu"];
				var txtspan = document.createElement("span");
				txtspan.setAttribute("class","green");
				var mtxt = document.createTextNode(ckb[i]["menu"]);
				txtspan.appendChild(mtxt);
				li.appendChild(cbox);
				li.appendChild(txtspan);
				list.appendChild(li);
			}else{
				var li = document.createElement("li");
				cbox = document.createElement("input");
				cbox.type = "checkbox";
				cbox.setAttribute("class","butt");
				cbox.checked = false;
				cbox.value = ckb[i]["menu"];
				var txtspan = document.createElement("span");
				txtspan.setAttribute("class","red");
				var mtxt = document.createTextNode(ckb[i]["menu"]);
				txtspan.appendChild(mtxt);
				li.appendChild(cbox);
				li.appendChild(txtspan);
				list.appendChild(li);
			}
		}	
		as = document.getElementsByClassName("butt");
		for(i=0;i<as.length;i++){
			as[i].addEventListener("click",function(evt){
				if(evt.target.checked == true){
					addon.port.emit("setActive",evt.target.value);
				}else{
					addon.port.emit("setInactive",evt.target.value);
				}
			},false)
		}
	});
}
</script>
</html>

Click to Install Most Recent Version

Advertisement
Standard

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s