Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added chrome extension to the code #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Check link stats by adding **+** to the URL. Example [pygy.co/pygmy+](https://py
- [How Link Stats Are Generated?](#how-link-stats-are-generated)
- [How Pygmy Auth Token Works?](#how-pygmy-auth-token-works)
- [Development](#development)
- [Browser extension](#browser-extension)
- [Development of the extension](#development-of-the-extension)
- [Contributions](#contributions)
- [Sponsorship](#sponsorship)
- [License](#license)
Expand Down Expand Up @@ -247,10 +249,24 @@ See coverage report(Coverage is bad because the coverage for integration tests i

`coverage report`

Browser extension
=================

Chrome extension is available on Chrome Store with the name: [your ShortURL service (pygmy)](https://chrome.google.com/webstore/detail/your-shorturl-service-pyg/kchefoihlnhfcaphckpopdknaolfegkp).

Development of the extension
----------------------------

* Enable development switch button on Crhome extension manager section
* Load extension from a folder
* Now you can start evolving the code and reload it with just one click on the Chrome extensions


Contributions
=============

Thanks [batarian71](https://github.com/batarian71) for providing the logo icon.
* Thanks [batarian71](https://github.com/batarian71) for providing the logo icon.
* [oriolrius](http://oriolrius.cat) Chrome extension.

Sponsorship
===========
Expand Down
7 changes: 7 additions & 0 deletions extensions/chrome/clipboard.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added extensions/chrome/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions extensions/chrome/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"manifest_version": 2,
"name": "your ShortURL service (pygmy)",
"version": "0.4",
"description": "pygmy shorturl Chrome extension",
"icons": {
"16": "icon.png"
},
"browser_action": {
"default_icon": "icon.png",
"default_title": "Create short URLs",
"default_popup": "popup.html"
},
"options_ui": {
"page": "options.html",
"open_in_tab": false
},
"permissions": [
"tabs",
"clipboardWrite",
"activeTab",
"storage"
]
}
17 changes: 17 additions & 0 deletions extensions/chrome/options.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<title>pymyg shorturl settings</title>
</head>
<body>
<div align="center">
<fieldset>
<label for="rest_url"><strong>pymyg service REST URL:</strong></label>
<input type="text" size="40" id="rest_url"><br />
<div id="status"></div>
<button id="save">Save</button>
</fieldset>
</div>
<script src="options.js"></script>
</body>
</html>
29 changes: 29 additions & 0 deletions extensions/chrome/options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Saves settings to chrome.storage
function save_options() {
var rest_url = document.getElementById('rest_url').value;
chrome.storage.sync.set({ 'restUrl': rest_url},
function() {
console.log('saved');
// Update status to let user know options were saved.
var status = document.getElementById('status');
status.innerHTML = '<br /><strong>Settings saved.</strong><br /><br />';
setTimeout(function() {
status.innerHTML = '';
}, 1500);
}
);
}

// Restore settings from chrome.storage
function restore_options() {
// Set default
chrome.storage.sync.get({ restUrl: 'https://api.url.joor.net'},
function(items) {
document.getElementById('rest_url').value = items.restUrl;
}
);
}

// linking events to the previous functions
document.addEventListener('DOMContentLoaded', restore_options);
document.getElementById('save').addEventListener('click', save_options);
23 changes: 23 additions & 0 deletions extensions/chrome/popup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html>
<head>
<title>ShortURL</title>
<script src="clipboard.min.js"></script>
<script src="popup.js"></script>
</head>
<body>
<div align="center">
<fieldset>
<label for="long-url"><strong>Current tab URL:</strong></label>
<input type="text" size="40" id="long-url" value="long url"><br />
<input type="button" id="short" value="Make Short"><br /><br />
<label for="short-url"><strong>Short URL:</strong></label>
<div id="short-url">--- not defined ---</div><br />
<input type="button" id="cc" data-clipboard-target="#short-url" value="Copy to clipboard"><br / -->
<div id="status"></div>
</fieldset>
<br />
<div id="backend"></div>
</div>
</body>
</html>
65 changes: 65 additions & 0 deletions extensions/chrome/popup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
var su = 'default value';

function makeShort(tablink) {
chrome.storage.sync.get({ restUrl: 'https://api.url.joor.net'},
function(items) {
var rest_url = items.restUrl;
document.getElementById("backend").innerHTML = '\
Backend used: <a href="'+rest_url+'">'+rest_url+'</a><br />\
Change the backend using extension options.';
rest_query( tablink, rest_url);
}
);
}

// REST client query
function rest_query(tablink, rest_url) {
var xhr = new XMLHttpRequest();
xhr.open("POST", rest_url + '/api/shorten', true);
xhr.setRequestHeader("Content-Type", "application/json");
var req = {name:"John Rambo", time:"2pm"};
var req = {
secret_key: "",
expire_after: null,
is_protected: false,
owner: null,
is_custom: false,
short_code: "",
long_url: tablink,
description: null
};
var data = JSON.stringify(req);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
var jresponse = JSON.parse(xhr.responseText);
su = jresponse.short_url; //+ '/' + jresponse.short_code;
var link = "<a href=\""+su+"\">"+su+"</a>";
document.getElementById("short-url").innerHTML = link;
document.getElementById("short-url").addEventListener('click', function() {
chrome.tabs.create({ url: su});
})
}
}
xhr.send(data);
}

chrome.tabs.getSelected(null,function(tab) {
var tablink = tab.url;
document.getElementById("long-url").setAttribute('value',tablink);
document.getElementById("short").onclick = function() {
makeShort(tablink);
};

var cc = new ClipboardJS('#cc');
cc.on('success', function(e) {
document.getElementById('status').innerHTML = '<br /><strong>Copied to clipboard.</strong><br /><br />';
setTimeout(function() {
document.getElementById('status').innerHTML = '';
}, 1500);
});
cc.on('error', function(e) {
console.error('Action:', e.action);
console.error('Trigger:', e.trigger);
});
});