Google Chrome扩展程序document.title无法正常工作 [英] Google Chrome extensions document.title not working
问题描述
这里是manifest.json中的代码
{
name:Page Title changer ,
version:1.0,
description:更改页面的< title>< / title>,
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';
});
我不明白为什么它不起作用,我在写这篇文章时检查了google代码文档 content_scripts.htmlrel =nofollow> documentation 你不能使用 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页面标题'},函数(响应){});
});
});
< / script>
changetitle.js
chrome.extension.onRequest.addListener(function(request,sender,sendResponse){
document.title = request.title;
});
您当然需要 tabs 权限才能使用此技术。
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.
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.
这篇关于Google Chrome扩展程序document.title无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!