Skip to content
Permalink
Browse files

Added Chrome extension

  • Loading branch information...
millinon committed Feb 18, 2017
1 parent 4057a62 commit dba7440e33e4cef6e7be4d68f667adb15ec08266
@@ -0,0 +1,11 @@
# Chrome link shortener

---

This directory contains source code for a Chrome extension that shortens URLs in a browser.

To try it out, go to [chrome://extensions](chrme://extensions), enable developer mode, and load this directory as an unpacked extension.

You may also download it from the chrome store [here](https://jump.wtf/chrome-extension).

It should work fine in Firefox as well, if you use [Chrome Store Foxified](https://addons.mozilla.org/en-Us/firefox/addon/chrome-store-foxified/).
Binary file not shown.
@@ -0,0 +1,20 @@
{
"manifest_version": 2,

"name": "jump.wtf Link Shortener",
"short_name": "jump.wtf",
"description": "This extension shortens a URL to a link that is easier to share.",
"version": "1.2",
"homepage_url": "https://jump.wtf",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html",
"default_title": "Shorten URL"
},
"permissions": [
"activeTab",
"clipboardWrite",
"https://jump.wtf/"
]
}

@@ -0,0 +1,38 @@
<!doctype html>
<html>
<head>
<title>jump.wtf Popup</title>
<style>
body {
font-family: "Segoe UI", "Lucida Grande", Tahoma, sans-serif;
font-size: 100%;
}
#status {
/* avoid an excessively wide status text */
white-space: pre;
text-overflow: ellipsis;
overflow: hidden;
max-width: 400px;
}
#message-div {
}
#btn-div {
text-align: center;
}
#button {
display: none;
margin: 0px auto;
}
</style>

<script src="popup.js"></script>
</head>
<body>
<div id="status">Please wait...</div>
<div id="message-div"><p id="message"></p><a id="link" target="_blank"></a></div>
<br />
<div id="btn-div"><button id="button" type="button">Copy to clipboard</button></div>
<img id="qr-code" hidden>
</body>
</html>

@@ -0,0 +1,67 @@
function shortenURL(url, callback, errCallback) {
var xhr = new XMLHttpRequest();

var data = JSON.stringify({
'action': 'genURL',
'input-url': url
});

xhr.open('POST', 'https://jump.wtf/a', true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.responseType = 'json';
xhr.onreadystatechange = function() {
if(xhr.readyState == 4){
var resp = xhr.response;
if(!resp){
errCallback("XHR failed.");
} else if(!resp.success){
errCallback(resp.message);
} else {
callback(resp.url);
}
}
};
xhr.timeout = 5000;
xhr.ontimeout = function(e){
errCallback("XHR timed out.");
};
xhr.send(data);
}

function renderStatus(statusText) {
document.getElementById('status').textContent = statusText;
}

document.addEventListener('DOMContentLoaded', function() {
chrome.tabs.query({active:true, currentWindow:true}, function(tabs) {
shortenURL(tabs[0].url, function(shortURL){
document.getElementById('status').textContent = 'URL Shortened';
document.getElementById('message').textContent = 'Your jump.wtf link: ';
var link = document.getElementById('link');
link.href = shortURL;
link.textContent = shortURL;

var img = document.getElementById('qr-code');
img.src = 'https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=' + encodeURIComponent(shortURL) + '&choe=UTF-8';
img.hidden = false;

document.getElementById('button').style.display = 'block';
}, function(error){
document.getElementById('status').textContent = 'Error';
document.getElementById('message').textContent = error;
});
});

document.getElementById('button').addEventListener('click', function (){
var text = document.getElementById('link').href;
var input = document.createElement('input');
input.style.position = 'fixed';
input.style.opacity = 0;
input.value = text;
document.body.appendChild(input);
input.select();
document.execCommand('Copy');
document.body.removeChild(input);
document.getElementById('btn-div').innerHTML = 'Link copied to clipboard.'
});
});

0 comments on commit dba7440

Please sign in to comment.
You can’t perform that action at this time.