Google Chrome 扩展 document.title 不起作用

发布于 2024-12-07 23:53:37 字数 539 浏览 0 评论 0 原文

这是manifest.json中的代码

{
  "name": "Page Title changer",
  "version": "1.0",
  "description": "Change the <title></title> of a page",
  "browser_action": {
    "default_icon": "icon.png"
  },
  "content_scripts": [
    {
      "matches": ["http://*/*"],
      "js": ["changetitle.js"]
    }
  ]
}

,这是changetitle.js文件中的代码

chrome.browserAction.onClicked.addListener(function() {
    document.title = 'new page title';
});

我不明白为什么它不起作用,我在编写此扩展时检查了谷歌代码文档。

here is the code in the manifest.json

{
  "name": "Page Title changer",
  "version": "1.0",
  "description": "Change the <title></title> of a page",
  "browser_action": {
    "default_icon": "icon.png"
  },
  "content_scripts": [
    {
      "matches": ["http://*/*"],
      "js": ["changetitle.js"]
    }
  ]
}

and here is the code from the changetitle.js file

chrome.browserAction.onClicked.addListener(function() {
    document.title = 'new page title';
});

i don't understand why it isn't working, i checked the google code docs while writing this extension.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

℉服软 2024-12-14 23:53:37

正如文档中详细说明的,您不能使用 chrome.* 内容脚本中的 API,除了某些 chrome.extension.* 方法。

不过,这并不会真正限制您,因为您可以使用消息传递来调用您的来自后台页面的内容脚本。例如;

background.html

<script type="application/javascript">
chrome.browserAction.onClicked.addListener(function() {
    chrome.tabs.getSelected(function (tab) {
        chrome.tabs.sendRequest(tab.id, {title: 'new page title'}, function (response) {});
    });
});
</script>

changetitle.js

chrome.extension.onRequest.addListener(function (request, sender, sendResponse) {
    document.title = request.title;
});

您当然需要 tabs 权限才能使用此技术。

As detailed in the documentation you cannot use chrome.* API within content scripts except for some chrome.extension.* methods.

However, this doesn't really limit you as you can use messaging to call your content script from your background page. For example;

background.html

<script type="application/javascript">
chrome.browserAction.onClicked.addListener(function() {
    chrome.tabs.getSelected(function (tab) {
        chrome.tabs.sendRequest(tab.id, {title: 'new page title'}, function (response) {});
    });
});
</script>

changetitle.js

chrome.extension.onRequest.addListener(function (request, sender, sendResponse) {
    document.title = request.title;
});

You will of course require the tabs permission in order to use this technique.

沒落の蓅哖 2024-12-14 23:53:37

这适用于任何 URI 方案

ma​​nifest.json

{
  "name": "Page Title changer",
  "version": "1.0",
  "description": "Change the <title></title> of a page",
  "browser_action": {
    "default_icon": "icon.png"
  },
  "background_page": "background.html",
  "permissions": [
        "tabs"
  ]
}

background.html

chrome.browserAction.onClicked.addListener(function () {
    chrome.tabs.executeScript(null, {
        file: "changetitle.js"
    });
});

changetitle.js

document.title = 'new page title';

This one will work in any URI Scheme

manifest.json

{
  "name": "Page Title changer",
  "version": "1.0",
  "description": "Change the <title></title> of a page",
  "browser_action": {
    "default_icon": "icon.png"
  },
  "background_page": "background.html",
  "permissions": [
        "tabs"
  ]
}

background.html

chrome.browserAction.onClicked.addListener(function () {
    chrome.tabs.executeScript(null, {
        file: "changetitle.js"
    });
});

changetitle.js

document.title = 'new page title';
紧拥背影 2024-12-14 23:53:37

试试这个:

chrome.browserAction.onClicked.addListener(function () {
    chrome.tabs.executeScript(null, {
        code: "document.title = 'new page title'"
    });
});

Try this:

chrome.browserAction.onClicked.addListener(function () {
    chrome.tabs.executeScript(null, {
        code: "document.title = 'new page title'"
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文