JAMES
JAMES

Reputation: 113

Chrome Extension Script

I've created a chrome extension which will find a USPS tracking number on text highlight. My current code is working great but I wanted to make some modification.

Here's the manifest.json

{
  "manifest_version": 2,
  "background" : { "scripts": ["background.js"] },
  "description": "Track on USPS",
  "icons": {
  "default_icon": "usps.png"
      },
  "minimum_chrome_version": "29.0", 
  "name": "USPS",
  "permissions": [ "contextMenus", "tabs", "http://*/*",
  "https://*/*" ],
  "version": "1.0"
}

This is the background.js:

/**
* Returns a handler which will open a new tab when activated.
*/
function searchgoogle(info)
{
  var searchstring = info.selectionText;
  chrome.tabs.create({url: "https://tools.usps.com/go/TrackConfirmAction?qtc_tLabels1=" + searchstring})
}

chrome.contextMenus.create({title: "Search USPS", contexts:["selection"], onclick: searchgoogle});

/**
* Create a context menu which will only show up for images.
*/
chrome.contextMenus.create({
  "title" : "Search tracking number on USPS",
  "type" : "normal",
  "contexts" : ["text"],
  "onclick" : getClickHandler()
});

Now I wanted to modify the current script:

https://tools.usps.com/go/TrackConfirmAction?qtc_tLabels1=" + searchstring

with the script below. This new code will open a pop up window. I tried modifying the new script but to no avail. Can anyone help me out?

Here's the new script that I wanted to use:

javascript:new function(){window.open('https://tools.usps.com/go/TrackConfirmAction?qtc_tLabels1=' + window.getSelection().toString(), '_blank', 'toolbar=0,location=0,menubar=0,top=91,height=900,width=650,left=1475');};

Thanks a lot in advance! This community help me a lot with my projects.

Upvotes: 3

Views: 200

Answers (2)

JAMES
JAMES

Reputation: 113

I finally figured it out!

function searchgoogle(info) {
var searchstring = info.selectionText;
chrome.windows.create({url: "https://tools.usps.com/go/TrackConfirmAction?qtc_tLabels1=" + searchstring})
}


chrome.contextMenus.create({
"title": "Search USPS",
"contexts":["selection"],
"onclick": searchgoogle
});

Upvotes: 1

Timothy Kanski
Timothy Kanski

Reputation: 1888

It doesn't look like you're actually calling the function you're creating in the script (and you can't because it isn't named). Try removing the function, and just executing the code:

javascript:window.open('https://tools.usps.com/go/TrackConfirmAction?qtc_tLabels1=' + window.getSelection().toString(), '_blank', 'toolbar=0,location=0,menubar=0,top=91,height=900,width=650,left=1475');

Upvotes: 2

Related Questions