Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
9inchurl.com extension parts [JavaScript][Web-Extension]
//Add context menu to links
var title = "Simple URL Extender";
var id = chrome.contextMenus.create({"title": title, "contexts":["link"], "onclick": onExtendClick});
//callback for when user clicks "Simple URL Extender" in context menu
function onExtendClick(info, tab) {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {getURL: true});
});
}
//Listener to get URL from content script(cs.js)
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if(request.url)
getExtendedURL(request.url);
});
//Gets the extended URL from 9inchurl.com and sends it to the content script(cs.js)
function getExtendedURL(shortenedURL){
var xhr = new XMLHttpRequest();
var url = "http://9inchurl.com/api.php?url=" + encodeURIComponent(shortenedURL);
xhr.onreadystatechange = function(){
if (xhr.readyState == 4) {
if(xhr.status == 200){
var data = xhr.responseText;
if(data == "update"){
chrome.notifications.create({type : "basic", iconUrl: "icon32.png", title: "Simple URL Extender", message: "This version is out of date. Please update the add-on to continue using it. Thanks!"});
}else{
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {extendedURL: data});
});
}
}else{
chrome.notifications.create({type : "basic", iconUrl: "icon48.png", title: "Simple URL Extender", message: "Could not extend URL. Please try again. If the problem persists, it might be because of temporary maintenance. Feedback/Support: 9inchurl@gmail.com"});
}
}
};
xhr.open("GET", url, true);
xhr.send();
}
var node = null;
//Finds the closest URL in DOM by traversing up through its ancestors in the DOM tree
function closestURL(node){
if(node.tagName == 'A' || node.tagName == 'a')
return node;
if(node.parentNode != null)
return closestURL(node.parentNode);
}
//Listens for right clicks and store the DOM node so we can get and modify it
document.addEventListener("mousedown", function(event){
if(event.button == 2){
node = closestURL(event.target);
console.log(node.href);
}
}, true);
//Used for sending/receiving URL to/from background.js
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if(request.getURL){ //send shortened URL to background script
chrome.runtime.sendMessage({url: node.href});
}
if(request.extendedURL){ //change old shortened URL and text to new extended URL
node.href = request.extendedURL;
node.textContent = request.extendedURL;
}
});
{
"manifest_version": 2,
"name": "Simple URL Extender",
"description": "Simply extend a shortened URL to its real URL by right clicking it and selecting 'Simple URL Extender'.",
"version": "2.0",
"permissions": ["contextMenus", "http://9inchurl.com/", "notifications"],
"author": "Kenneth",
"background": {
"scripts": ["background.js"]
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["cs.js"]
}
],
"icons": { "16": "icon16.png",
"32": "icon32.png",
"48": "icon48.png",
"128": "icon128.png"
}
}
@eladkarako

This comment has been minimized.

Copy link
Owner Author

commented Jul 7, 2019

taken from the official web-extension of 9inchurl.com ,
the API is quite simple to use, you can try to use it yourself but avoid abusing the service,
this is one of the only ones exists now days....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.