首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Chrome扩展:使用chrome.tabs和chrome.contextMenus

Chrome扩展:使用chrome.tabs和chrome.contextMenus
EN

Stack Overflow用户
提问于 2011-08-24 03:10:13
回答 1查看 662关注 0票数 0

我正在尝试创建一个chrome扩展,它可以删除URL并在一个新的选项卡中打开URL。但是,我始终得到与this (content_script error)相同的错误。我已经按照指示行事了,但我相信我只是不明白自己哪里出了问题。以下是完整的代码:

manifest.json

代码语言:javascript
复制
{
  "name": "Link scrub",  
  "description": "Removes redirectors from links", 
  "version": "0.1",
  "permissions": ["contextMenus", "tabs"],
  "background_page" : "background.html"
  "content_scripts": [{
    "js" : ["linkscrub.js"]
  }];
}    

linkscrub.js

代码语言:javascript
复制
chrome.contextMenus.create({
    "title" : "Link Trap",
    "type" : "normal",
    "contexts" : ["link"],
    "onclick" : modifyLink
});
 function modifyLink(info, tab) {
    chrome.extension.sendRequest({
        "nurl" = info.linkURL,
        function(response) {
            console.log("linkscrub failed: " + response.farewell)
            }
    });
}

background.html

代码语言:javascript
复制
<script>
chrome.extension.onRequest.addListener(
  function(request, sender, sendResponse) {
    link = "";
    link = sender.nurl;
    link = link.match("url=\b(.*?)&link");
    chrome.tabs.create({
        "url": link,
        "selected" : false
    });
    if(chrome.extension.lastError) 
        sendResponse({farewell : chrome.extension.lastError.message});
    else
        sendResponse({farewell : "Success")};    
  });
<script>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-08-24 03:16:33

它会引发错误,因为您不能在内容脚本中使用chrome.contextMenus.* API。

您不需要用于此任务的内容脚本,只需将所有内容从linkscrub.js移到后台页面(也不需要这些请求)。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7170013

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档