简体   繁体   English

从网页链接到 chrome:// url

[英]Link to chrome:// url from a webpage

UPDATE更新

chrome://about/ has direct links to chrome://settings and others, so it can be done. chrome://about/有指向chrome://settings和其他的直接链接,所以它可以完成。

How can I link/redirect to a chrome:// URL in a webpage?如何链接/重定向到网页中的chrome:// URL? For example, chrome://settings .例如, chrome://settings When attempting to click a link to a chrome:// URL,尝试单击chrome:// URL 的链接时,

<a href="chrome://settings">link</a>

I get an error in the console:我在控制台中收到错误消息:

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

According to this answer, it is possible:根据这个答案,有可能:

Link to chrome://chrome/extensions from extension options page 从扩展选项页面链接到 chrome://chrome/extensions

I've tried this script:我试过这个脚本:

<a href="#" id="test">test</a>
<script>
document.addEventListener('DOMContentLoaded', function () {
    document.getElementById('test').addEventListener('click', function () {
        chrome.tabs.update({
            url: 'chrome://chrome/extensions'
        });
    });
});
</script>

And also this one:还有这个:

<a href="#" onclick="chrome.tabs.create({url:'chrome://settings/clearBrowserData'});">click me</a>

Neither works.两者都不起作用。 In addition to those, I've tried:除了这些,我还尝试过:

  • Linking to it using <a href="chrome://newtab">使用<a href="chrome://newtab">链接到它
  • Redirect the user using window.location .使用window.location重定向用户。

However, I believe there may be a JavaScript workaround.但是,我相信可能有一个 JavaScript 解决方法。 When the user presses a link, is it possible to change the URL to chrome://settings ?当用户按下链接时,是否可以将 URL 更改为chrome://settings The user would have to press Enter after, but this seems like the best I'd get.用户必须在之后按Enter 键,但这似乎是我得到的最好的结果。

Nope, there is no way to do it from a webpage.不,没有办法从网页上做到这一点。

chrome:// is a privileged origin, and any attempt to open it will result in a redirect to about:blank . chrome://是一个特权来源,任何打开它的尝试都会导致重定向到about:blank

Chrome does that to reduce the attack surface: even though just opening those URLs should be harmless, it's better not to allow websites and external programs to even try. Chrome 这样做是为了减少攻击面:尽管打开这些 URL应该是无害的,但最好不要让网站和外部程序尝试。

Besides, it's not that harmless, eg debug URLs like chrome://crash , chrome://hang (careful, those really do what you expect).此外,它也不是那么无害,例如调试像chrome://crashchrome://hang这样的 URL(小心,它们确实按照您的预期运行)。

Obviously, you can't use chrome.tabs as it's an extension API not exposed to websites.显然,您不能使用chrome.tabs因为它是一个未向网站公开的扩展 API。 It is, however, capable of opening privileged origins.然而,它能够打开特权起源。

Below works for me;以下对我有用;

manifest.json清单文件.json

 {
  "manifest_version": 2,

  "name": "Redirection Sample",
  "description": "Test Redirection",
  "version": "1.0",

  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "permissions": [
    "activeTab",
    "https://ajax.googleapis.com/"
  ]
}

popup.html弹出窗口.html

<!doctype html>
<html>
  <head>
    <title>Redirection Popup</title>
    <script src="popup.js"></script>
  </head>
  <body>
  <a href="#" id="test">test</a>
  </body>
</html>

popup.js弹出窗口.js

  document.addEventListener('DOMContentLoaded', function() {
        document.getElementById('test').addEventListener('click', function() {
            chrome.tabs.update({ url: 'chrome://chrome/extensions' });
        });
    });

icon.png图标.png

Add a customized one or download a sample from this link添加定制的或从此链接下载示例

Include all these 4 inside one directory and load unpack the extension to chrome.将所有这 4 个包含在一个目录中,并将扩展程序加载到 chrome 中。 It should work!它应该工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM