【问题标题】:Linking to chrome://history through extension通过扩展链接到 chrome://history
【发布时间】:2015-04-14 19:59:43
【问题描述】:

我正在开发一个 chrome 扩展程序,它通过内容脚本将几个按钮注入页面,我想要一个链接到历史页面的按钮(chrome://history )。

这只会打开一个没有 URL 的空白标签:

window.open("chrome://history", "_blank");

并且有一个指向历史页面的超链接直接给我一个错误:

<a href="chrome://history" target="_blank">...</a>

Not allowed to load local resource: chrome://history/

Chrome's Extension API 没有将chrome://* 列为扩展页面的支持方案,这解释了错误,但我要做的就是提供指向历史页面的链接。有没有办法做到这一点,即使它有点(或很多)令人费解?

谢谢!

编辑:补充说这是通过内容脚本完成的,我没有在最初的帖子中澄清这一点。

【问题讨论】:

  • 澄清一下:这是在内容脚本中完成的。

标签: javascript jquery google-chrome-extension url-scheme


【解决方案1】:

Wolf War 的回答是正确的,但是,对于内容脚本,需要额外的步骤。

您需要一个background(或更好的event)页面来处理请求,因为内容脚本无法调用该API。你需要使用Messaging:

// Content script
// element is your DOM element
element.addEventListener("click", function(e) {
  chrome.runtime.sendMessage({command: "openHistory"});
});

// Event script
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
  if(command == "openHistory") {
    chrome.tabs.create({'url': 'chrome://history'});
  }
});

【讨论】:

  • 这正是我让它工作的原因!你在我发帖时回答了这个问题,谢谢! :D
【解决方案2】:

在 Chrome 中打开带有历史内部页面的新活动标签的代码

chrome.tabs.create({'url': 'chrome://history', 'active': true});

【讨论】:

  • 我没有说明这是一个内容脚本。 This thread 表示内容脚本无法访问除 chrome.extension 的部分之外的任何扩展 API。当我运行它时,它给了我这个错误:Cannot read property 'create' of undefined
  • @JosephNewman 您必须向后台/事件页面发送消息才能执行此操作。
【解决方案3】:

Wolf War's response 让我想到了向我的扩展程序本身发送消息并让它从那里启动选项卡,这很有效。

我将此添加到我的内容脚本中,该脚本会在页面上注入按钮:

$("#history_button").click( function() {
    chrome.runtime.sendMessage({
        type: "OPEN_HISTORY"
    });
});

并在我的扩展程序的background script 中添加了一个相应的侦听器来创建选项卡:

chrome.runtime.onMessage.addListener( function(request) {
    if (request.type == "OPEN_HISTORY") {
        chrome.tabs.create({ "url": "chrome://history", "active": true });
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-19
    • 1970-01-01
    • 2011-12-17
    • 2022-08-08
    • 2014-10-01
    • 1970-01-01
    • 2018-08-29
    相关资源
    最近更新 更多