Chrome扩展程序脚本

时间:2016-04-29 23:13:12

标签: javascript google-chrome-extension

我已经创建了一个chrome扩展程序,可以在文本突出显示上找到USPS跟踪编号。我目前的代码工作得很好,但我想做一些修改。

这是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"
}

这是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()
});

现在我想修改当前的脚本:

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

使用下面的脚本。这个新代码将打开一个弹出窗口。我尝试修改新脚本但无济于事。任何人都可以帮助我吗?

这是我想要使用的新脚本:

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');};

提前多多感谢!这个社区对我的项目帮助很大。

2 个答案:

答案 0 :(得分:2)

看起来你实际上并没有调用你在脚本中创建的功能(而且你不能因为它没有被命名)。尝试删除该功能,然后执行代码:

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');

答案 1 :(得分:1)

我终于明白了!

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
});
相关问题