First off, I'm new to making chrome extensions, so don't assume I know a whole lot. For the extension I'm making the user needs to be able to right click on a link, select a context menu item, and the extension needs to get sent the final url of that link.
Specifically amazon affiliate links. So for example the following:
Would need to get converted to:
http://www.amazon.com/gp/product/0470281731/ref=as_li_ss_tl?ie=UTF8&fpl=fresh&pf_rd_m=ATVPDKIKX0DER&pf_rd_s=desktop-1&pf_rd_r=0WRZW1V7VVDJWS54WCM4&..... blah blah blah
I've looked around and I can't find any answers. Am I SOL?
The code I have so far is pretty basic:
//background.js
chrome.runtime.onInstalled.addListener(function() {
chrome.contextMenus.create({
title: 'Add this Link',
id: 'linkContext',
contexts: ['link'],
});
});
chrome.contextMenus.onClicked.addListener(function(data, tab) {
if (data.menuItemId === "linkContext") {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id,
{
linkUrl: data.linkUrl,
},
function(response) {
alert(response.host);
});
});
}
});
chrome.runtime.onMessage.addListener(
//content_script.js
function(request, sender, sendResponse) {
if (request.linkUrl){
pathArray = request.linkUrl.split( '/' );
protocol = pathArray[0];
host = pathArray[2];
url = protocol + '//' + host;
sendResponse({host: host});
}
});
//manifest.json
{
"name": "jQuery DOM",
"manifest_version": 2,
"version": "1.0",
"description": "Manipulate the DOM when the page is done loading",
"browser_action": {
"name": "Manipulate DOM",
"icons": ["icon.png"],
"default_icon": "icon.png"
},
"background": {
"scripts": ["background.js"],
"persistent": false
},
"permissions": [
"contextMenus"
],
"content_scripts": [ {
"js": [ "jquery.min.js", "content_script.js" ],
"matches": [ "http://*/*", "https://*/*"]
}],
"web_accessible_resources":[
"menu.html",
"menu.css"
]
}
Like I said I'm pretty new to this, so I'm unsure of how to proceed. I'd like to do some parsing of the "final url" so I can present information about it to the user. I.E. the Affiliate ID. But for that I can't use the shortened link from above.