如何在上下文菜单中添加书签?

时间:2012-05-23 16:31:22

标签: google-chrome google-chrome-extension

我想在上下文菜单中添加一个书签。

这是我的书签:

  

javascript:(function(){var a = window.open('http://localhost/test/mm.php?title='+ encodeURIComponent(document.title),'test','left ='+((window.screenX || window。 screenLeft)50)+”,顶部= '+((window.screenY || window.screenTop)50)+',高度= 300像素,宽= 700像素,可调整大小= 1,alwaysRaised = 1,位置= 1,链接= 0,滚动条= 0,工具栏= 0' ); window.setTimeout(函数(){a.focus()},300)})();

这是我的代码:

function getClickHandler() {
  return function(info, tab) {

  };
};

chrome.contextMenus.create({
  "title" : "Hello",
  "type" : "normal",
  "onclick" : getClickHandler()
});

但是现在,我不知道在哪里插入书签。

@ wong2:

这是我的新getClickHandler,但它不起作用:

function getClickHandler() {
  return function(info, tab) {
      chrome.tabs.executeScript(tab.id, {
          code: "(function(){var a=window.open('http://localhost/test/mm.php?title='+encodeURIComponent(document.title),'test','left='+((window.screenX||window.screenLeft)+50)+',top='+((window.screenY||window.screenTop)+50)+',height=300px,width=700px,resizable=1,alwaysRaised=1,location=1,links=0,scrollbars=0,toolbar=0');window.setTimeout(function(){a.focus()},300)})();"
      });
  };
};

的manifest.json:

{
  "name" : "testtt",
  "version" : "1.0.1",
  "description" : "test button",
  "background" : { "scripts": ["background.js"] },
  "permissions" : [
    "contextMenus",
    "tabs",
    "http://*/*",
    "https://*/*"
   ],
  "minimum_chrome_version" : "6.0.0.0",
  "icons" : {
    "16" : "imageinfo-16.png",
    "48" : "imageinfo-48.png",
    "128" : "imageinfo-128.png"
  },
  "manifest_version": 2
}

1 个答案:

答案 0 :(得分:1)

您可以尝试chrome.tabs.executeScript将代码插入页面:

function getClickHandler() {
  return function(info, tab) {
      chrome.tabs.executeScript(tab.id, {
          code: YOUR_CODE_IN_STRING_HERE
      });
  };
};