2011-10-05 130 views
4

這裏是在manifest.json谷歌Chrome擴展document.title時不工作

{ 
    "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'; 
}); 

我不明白爲什麼它不工作,我檢查了谷歌代碼文檔在寫這個擴展。

+0

如果你想設置'document.title',你必須有'' – <span class="text-secondary"> <small> <span></span> </small> </span> </p> </div> </div> </div> </div> </div> </article> </div> <div class="answer-title"> <span class="text-logo margin-top-sm">A</span> <h2 class="title h4">回答</h2> </div> <div class="item-description text-md markdown-body margin-bottom-40 voidso"> <article class="board-top-1 padding-top-10"> <div class="post-col vote-info"> <span class="count">3<i class="fa fa-thumbs-up"></i></span> <i class="fa fa-check fa-2x"></i> </div> <div class="post-offset"> <div class="answer fmt"> <p>由於中詳述的<a href="http://code.google.com/chrome/extensions/content_scripts.html" rel="nofollow">documentation</a>你不能用內容腳本中<code class="prettyprint-override">chrome.*</code> API除了一些<code class="prettyprint-override">chrome.extension.*</code>方法。</p> <p>然而,這並沒有真正限制你,你可以使用<a href="http://code.google.com/chrome/extensions/messaging.html" rel="nofollow">messaging</a>從你的背景頁面中調用您的內容腳本。例如;</p> <p><strong>background.html</strong></p> <pre><code class="prettyprint-override"><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> </code></pre> <p><strong>changetitle.js</strong></p> <pre><code class="prettyprint-override">chrome.extension.onRequest.addListener(function (request, sender, sendResponse) { document.title = request.title; }); </code></pre> <p>你當然需要爲了使用這種技術的<a href="http://code.google.com/chrome/extensions/tabs.html#manifest" rel="nofollow">tabs</a>許可。</p> </div> <div class="post-info"> <div class="post-meta row"> <p class="text-secondary col-lg-6"> <span class="source"> <a rel="noopener" target="_blank" href="https://stackoverflow.com/q/7657988">來源</a> </span> </p> <p class="text-secondary col-lg-6"> <span class="float-right date"> <span>2011-10-05 07:27:05</span> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/449605/">Alasdair</a></span> </p> <p class="col-12"></p> <p class="col-12"></p></div> </div> </div> </article> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-6208739752673518" data-ad-slot="1038284119" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <article class="board-top-1 padding-top-10"> <div class="post-col vote-info"> <span class="count">1<i class="fa fa-thumbs-up"></i></span> </div> <div class="post-offset"> <div class="answer fmt"> <p>試試這個:</p> <pre><code class="prettyprint-override">chrome.browserAction.onClicked.addListener(function() { chrome.tabs.executeScript(null, { code: "document.title = 'new page title'" }); }); </code></pre> </div> <div class="post-info"> <div class="post-meta row"> <p class="text-secondary col-lg-6"> <span class="source"> <a rel="noopener" target="_blank" href="https://stackoverflow.com/q/7655637">來源</a> </span> </p> <p class="text-secondary col-lg-6"> <span class="float-right date"> <span>2011-10-05 00:48:51</span> </p> <p class="col-12"></p> <p class="col-12"></p></div> </div> <!-- comments --> <div class="comments"> <div itemprop="comment" class="post-comment"> <div class="row"> <div class="col-lg-1"><span class="text-secondary">+0</span></div> <div class="col-lg-11"> <p class="commenttext">這沒有奏效 – <span class="text-secondary"> <small> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/1269169/">Samuel</a></span> <span></span> </small> </span> </p> </div> </div> </div> </div> </div> </article> <article class="board-top-1 padding-top-10"> <div class="post-col vote-info"> <span class="count">2<i class="fa fa-thumbs-up"></i></span> </div> <div class="post-offset"> <div class="answer fmt"> <p>這人會在任何URI方案</p> <p><strong>manifest.json的工作</strong></p> <pre><code class="prettyprint-override">{ "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" ] } </code></pre> <p><strong>background.html</strong></p> <pre><code class="prettyprint-override">chrome.browserAction.onClicked.addListener(function() { chrome.tabs.executeScript(null, { file: "changetitle.js" }); }); </code></pre> <p><strong>changetitle.js</strong></p> <pre><code class="prettyprint-override">document.title = 'new page title'; </code></pre> </div> <div class="post-info"> <div class="post-meta row"> <p class="text-secondary col-lg-6"> <span class="source"> <a rel="noopener" target="_blank" href="https://stackoverflow.com/q/7665960">來源</a> </span> </p> <p class="text-secondary col-lg-6"> <span class="float-right date"> <span>2011-10-05 18:33:00</span> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/741981/">cvsguimaraes</a></span> </p> <p class="col-12"></p> <p class="col-12"></p></div> </div> </div> </article> </div> <div class="clearfix"> </div> <div class="relative-box"> <div class="relative">相關問題</div> <ul class="relative_list"> <li> 1. <a href="http://hk.uwenku.com/question/p-fndjsivb-nw.html" target="_blank" title="谷歌Chrome擴展"> 谷歌Chrome擴展 </a> </li> <li> 2. <a href="http://hk.uwenku.com/question/p-uaguytqj-cq.html" target="_blank" title="谷歌Chrome Socket API擴展"> 谷歌Chrome Socket API擴展 </a> </li> <li> 3. <a href="http://hk.uwenku.com/question/p-uuspgtnp-hn.html" target="_blank" title="在谷歌Chrome擴展"> 在谷歌Chrome擴展 </a> </li> <li> 4. <a href="http://hk.uwenku.com/question/p-turctpmv-bhn.html" target="_blank" title="谷歌Chrome擴展 - 輸入驗證工作不"> 谷歌Chrome擴展 - 輸入驗證工作不 </a> </li> <li> 5. <a href="http://hk.uwenku.com/question/p-tevxvedu-bhg.html" target="_blank" title="谷歌Chrome擴展:webdb或不?"> 谷歌Chrome擴展:webdb或不? </a> </li> <li> 6. <a href="http://hk.uwenku.com/question/p-bmpkdyrz-bo.html" target="_blank" title="谷歌Chrome擴展:不拋出錯誤?"> 谷歌Chrome擴展:不拋出錯誤? </a> </li> <li> 7. <a href="http://hk.uwenku.com/question/p-btfrbrxu-tu.html" target="_blank" title="Chrome擴展popup.js不工作"> Chrome擴展popup.js不工作 </a> </li> <li> 8. <a href="http://hk.uwenku.com/question/p-ojbztcbg-g.html" target="_blank" title="翻譯谷歌Chrome擴展HTML表單"> 翻譯谷歌Chrome擴展HTML表單 </a> </li> <li> 9. <a href="http://hk.uwenku.com/question/p-fulwbufr-n.html" target="_blank" title="谷歌Chrome擴展 - 重定向"> 谷歌Chrome擴展 - 重定向 </a> </li> <li> 10. <a href="http://hk.uwenku.com/question/p-axnhdzhm-mk.html" target="_blank" title="離線谷歌Chrome瀏覽器擴展"> 離線谷歌Chrome瀏覽器擴展 </a> </li> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-6208739752673518" data-ad-slot="4606349252"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <li> 11. <a href="http://hk.uwenku.com/question/p-dkzwkleo-ben.html" target="_blank" title="自動化谷歌Chrome擴展包裝"> 自動化谷歌Chrome擴展包裝 </a> </li> <li> 12. <a href="http://hk.uwenku.com/question/p-uwuskino-hy.html" target="_blank" title="更改谷歌Chrome網頁的擴展"> 更改谷歌Chrome網頁的擴展 </a> </li> <li> 13. <a href="http://hk.uwenku.com/question/p-yfwpuaec-hp.html" target="_blank" title="谷歌Chrome擴展的Facebook JavaScript API"> 谷歌Chrome擴展的Facebook JavaScript API </a> </li> <li> 14. <a href="http://hk.uwenku.com/question/p-mmzgxrma-mn.html" target="_blank" title="谷歌Chrome擴展怪異渲染"> 谷歌Chrome擴展怪異渲染 </a> </li> <li> 15. <a href="http://hk.uwenku.com/question/p-bpzybtxg-bek.html" target="_blank" title="谷歌Chrome擴展HTTPS AJAX請求"> 谷歌Chrome擴展HTTPS AJAX請求 </a> </li> <li> 16. <a href="http://hk.uwenku.com/question/p-qvzppdwf-bmh.html" target="_blank" title="谷歌Chrome擴展包錯誤"> 谷歌Chrome擴展包錯誤 </a> </li> <li> 17. <a href="http://hk.uwenku.com/question/p-xuiguied-ep.html" target="_blank" title="谷歌Chrome擴展webrequest攔截"> 谷歌Chrome擴展webrequest攔截 </a> </li> <li> 18. <a href="http://hk.uwenku.com/question/p-zjbbduer-gk.html" target="_blank" title="谷歌Chrome擴展POST VS GET"> 谷歌Chrome擴展POST VS GET </a> </li> <li> 19. <a href="http://hk.uwenku.com/question/p-rqjmqdhz-ou.html" target="_blank" title="谷歌翻譯API的Chrome擴展"> 谷歌翻譯API的Chrome擴展 </a> </li> <li> 20. <a href="http://hk.uwenku.com/question/p-nvabggjo-bme.html" target="_blank" title="值無效:谷歌Chrome擴展"> 值無效:谷歌Chrome擴展 </a> </li> <li> 21. <a href="http://hk.uwenku.com/question/p-cbpjrjqu-gt.html" target="_blank" title="Chrome擴展的內容腳本不工作在谷歌,YouTube的頁面"> Chrome擴展的內容腳本不工作在谷歌,YouTube的頁面 </a> </li> <li> 22. <a href="http://hk.uwenku.com/question/p-rwabrift-sz.html" target="_blank" title="Chrome阻止擴展工作"> Chrome阻止擴展工作 </a> </li> <li> 23. <a href="http://hk.uwenku.com/question/p-vfqemhoc-bdk.html" target="_blank" title="jQuery是不是在谷歌chrome工作"> jQuery是不是在谷歌chrome工作 </a> </li> <li> 24. <a href="http://hk.uwenku.com/question/p-calmgvof-bkm.html" target="_blank" title="Mjpeg流媒體不工作谷歌Chrome"> Mjpeg流媒體不工作谷歌Chrome </a> </li> <li> 25. <a href="http://hk.uwenku.com/question/p-frznlpzx-vk.html" target="_blank" title="JavaScript的不工作的谷歌chrome"> JavaScript的不工作的谷歌chrome </a> </li> <li> 26. <a href="http://hk.uwenku.com/question/p-fyfddurt-mx.html" target="_blank" title="Chrome擴展xmlHttp請求不工作"> Chrome擴展xmlHttp請求不工作 </a> </li> <li> 27. <a href="http://hk.uwenku.com/question/p-wrlksniz-gt.html" target="_blank" title="Chrome擴展JavaScript的不工作"> Chrome擴展JavaScript的不工作 </a> </li> <li> 28. <a href="http://hk.uwenku.com/question/p-kcbdfjjf-bge.html" target="_blank" title="Chrome擴展程序PHP不工作"> Chrome擴展程序PHP不工作 </a> </li> <li> 29. <a href="http://hk.uwenku.com/question/p-ujfbqmeq-ze.html" target="_blank" title="使用Chrome擴展不工作"> 使用Chrome擴展不工作 </a> </li> <li> 30. <a href="http://hk.uwenku.com/question/p-mytvofxx-vs.html" target="_blank" title="Chrome擴展使用jQuery不工作"> Chrome擴展使用jQuery不工作 </a> </li> </ul> </div> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-6208739752673518" data-ad-slot="1575177025"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <div class="padding-top-10"></div> </div> </div> <script type="text/javascript" src="http://img.uwenku.com/uwenku/script/side.js?t=1644592048261"></script> <script type="text/javascript" src="http://img.uwenku.com/uwenku/plugin/highlight/highlight.pack.js"></script> <link href="http://img.uwenku.com/uwenku/plugin/highlight/styles/docco.css" media="screen" rel="stylesheet" type="text/css" /> <script type="text/javascript"> $('pre').each(function(i, e) { hljs.highlightBlock(e, "<span class='indent'> </span>", false) }); </script> <div class="col-lg-3 col-md-4 col-sm-5"> <div id="rightTop"> <div class="row"> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-6208739752673518" data-ad-slot="5415218910" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <div class="row sidebar panel panel-default"> <div class="panel-heading font-bold"> 最新問題 </div> <div class="m-b-sm m-t-sm clearfix"> <ul class="side_article_list"> <li class="side_article_list_item"> 1. <a href="http://hk.uwenku.com/question/p-bunfgcwj-uo.html" target="_blank" title="Python:從每個打開的Google Chrome標籤獲取所有Url"> Python:從每個打開的Google Chrome標籤獲取所有Url </a> </li> <li class="side_article_list_item"> 2. <a href="http://hk.uwenku.com/question/p-umizetvh-tw.html" target="_blank" title="當它們在循環ng-bootstrap中時關閉datepicker"> 當它們在循環ng-bootstrap中時關閉datepicker </a> </li> <li class="side_article_list_item"> 3. <a href="http://hk.uwenku.com/question/p-txdnyrmw-ue.html" target="_blank" title="如何呈現多次相同的組件和所有實例具有相同的狀態"> 如何呈現多次相同的組件和所有實例具有相同的狀態 </a> </li> <li class="side_article_list_item"> 4. <a href="http://hk.uwenku.com/question/p-pvxpsmkq-vq.html" target="_blank" title="pytesseract結果與tesseract命令行結果不同"> pytesseract結果與tesseract命令行結果不同 </a> </li> <li class="side_article_list_item"> 5. <a href="http://hk.uwenku.com/question/p-uvaagzxv-va.html" target="_blank" title="測試時抑制記錄器"> 測試時抑制記錄器 </a> </li> <li class="side_article_list_item"> 6. <a href="http://hk.uwenku.com/question/p-biwzksxc-ss.html" target="_blank" title="如何平均一組圖像並使用MATLAB將平均圖像保存爲平均圖像"> 如何平均一組圖像並使用MATLAB將平均圖像保存爲平均圖像 </a> </li> <li class="side_article_list_item"> 7. <a href="http://hk.uwenku.com/question/p-zhdytuai-tb.html" target="_blank" title="ListView適配器不工作(Quickblox聊天應用程序)"> ListView適配器不工作(Quickblox聊天應用程序) </a> </li> <li class="side_article_list_item"> 8. <a href="http://hk.uwenku.com/question/p-bdtzveno-tn.html" target="_blank" title="如何使用VBA從特定的Excel單元格迭代到此列中具有值的最新行?"> 如何使用VBA從特定的Excel單元格迭代到此列中具有值的最新行? </a> </li> <li class="side_article_list_item"> 9. <a href="http://hk.uwenku.com/question/p-ssyneghi-qh.html" target="_blank" title="IOS調整滾動視圖通過containerview裏面"> IOS調整滾動視圖通過containerview裏面 </a> </li> <li class="side_article_list_item"> 10. <a href="http://hk.uwenku.com/question/p-oukhnytl-qt.html" target="_blank" title="如何在平行版中調試Windows UWP應用程序?"> 如何在平行版中調試Windows UWP應用程序? </a> </li> </ul> </div> </div> </div> <p class="article-nav-bar"></p> <div class="row sidebar article-nav"> <div class="row box_white visible-sm visible-md visible-lg margin-zero"> <div class="top"> <h3 class="title"><i class="glyphicon glyphicon-th-list"></i> 相關問題</h3> </div> <div class="article-relative-content"> <ul class="side_article_list"> <li class="side_article_list_item"> 1. <a href="http://hk.uwenku.com/question/p-fndjsivb-nw.html" target="_blank" title="谷歌Chrome擴展"> 谷歌Chrome擴展 </a> </li> <li class="side_article_list_item"> 2. <a href="http://hk.uwenku.com/question/p-uaguytqj-cq.html" target="_blank" title="谷歌Chrome Socket API擴展"> 谷歌Chrome Socket API擴展 </a> </li> <li class="side_article_list_item"> 3. <a href="http://hk.uwenku.com/question/p-uuspgtnp-hn.html" target="_blank" title="在谷歌Chrome擴展"> 在谷歌Chrome擴展 </a> </li> <li class="side_article_list_item"> 4. <a href="http://hk.uwenku.com/question/p-turctpmv-bhn.html" target="_blank" title="谷歌Chrome擴展 - 輸入驗證工作不"> 谷歌Chrome擴展 - 輸入驗證工作不 </a> </li> <li class="side_article_list_item"> 5. <a href="http://hk.uwenku.com/question/p-tevxvedu-bhg.html" target="_blank" title="谷歌Chrome擴展:webdb或不?"> 谷歌Chrome擴展:webdb或不? </a> </li> <li class="side_article_list_item"> 6. <a href="http://hk.uwenku.com/question/p-bmpkdyrz-bo.html" target="_blank" title="谷歌Chrome擴展:不拋出錯誤?"> 谷歌Chrome擴展:不拋出錯誤? </a> </li> <li class="side_article_list_item"> 7. <a href="http://hk.uwenku.com/question/p-btfrbrxu-tu.html" target="_blank" title="Chrome擴展popup.js不工作"> Chrome擴展popup.js不工作 </a> </li> <li class="side_article_list_item"> 8. <a href="http://hk.uwenku.com/question/p-ojbztcbg-g.html" target="_blank" title="翻譯谷歌Chrome擴展HTML表單"> 翻譯谷歌Chrome擴展HTML表單 </a> </li> <li class="side_article_list_item"> 9. <a href="http://hk.uwenku.com/question/p-fulwbufr-n.html" target="_blank" title="谷歌Chrome擴展 - 重定向"> 谷歌Chrome擴展 - 重定向 </a> </li> <li class="side_article_list_item"> 10. <a href="http://hk.uwenku.com/question/p-axnhdzhm-mk.html" target="_blank" title="離線谷歌Chrome瀏覽器擴展"> 離線谷歌Chrome瀏覽器擴展 </a> </li> </ul> </div> </div> </div> </div> </div> </div> </div><!-- wrap end--> <!-- footer --> <footer id="footer"> <div class="bg-simple lt"> <div class="container"> <div class="row padder-v m-t"> <div class="col-xs-8"> <ul class="list-inline"> <li><a href="http://hk.uwenku.com/contact">聯系我們</a></li> <li>© 2020 HK.UWENKU.COM</li> <li><a target="_blank" href="https://beian.miit.gov.cn/">沪ICP备13005482号-4</a></li> <li><script type="text/javascript" src="https://v1.cnzz.com/z_stat.php?id=1280101193&web_id=1280101193"></script></li> <li><a href="http://www.uwenku.com/" target="_blank" title="优文库">简体中文</a></li> <li><a href="http://hk.uwenku.com/" target="_blank" title="優文庫">繁體中文</a></li> <li><a href="http://ru.uwenku.com/" target="_blank" title="поле вопросов и ответов">Русский</a></li> <li><a href="http://de.uwenku.com/" target="_blank" title="Frage - und - antwort - Park">Deutsch</a></li> <li><a href="http://es.uwenku.com/" target="_blank" title="Preguntas y respuestas">Español</a></li> <li><a href="http://hi.uwenku.com/" target="_blank" title="कार्यक्रम प्रश्न और उत्तर पार्क">हिन्दी</a></li> <li><a href="http://it.uwenku.com/" target="_blank" title="IL Programma di chiedere Park">Italiano</a></li> <li><a href="http://ja.uwenku.com/" target="_blank" title="プログラム問答園区">日本語</a></li> <li><a href="http://ko.uwenku.com/" target="_blank" title="프로그램 문답 단지">한국어</a></li> <li><a href="http://pl.uwenku.com/" target="_blank" title="program o park">Polski</a></li> <li><a href="http://tr.uwenku.com/" target="_blank" title="Program soru ve cevap parkı">Türkçe</a></li> <li><a href="http://vi.uwenku.com/" target="_blank" title="Đáp ứng viên">Tiếng Việt</a></li> <li><a href="http://fr.uwenku.com/" target="_blank" title="Programme interrogation Park">Française</a></li> </ul> </div> </div> </div> </div> </div> </footer> <!-- / footer --> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?f78a970f17b19a79fc477a3378096f29"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> </body> </html>