[英]Replacing the Google logo with a Chrome extension
所以我做了一個擴展,它應該用另一張圖片替換搜索結果頁面的谷歌標志。 由於徽標的 img 標簽沒有自己的 class 或 id,我不得不通過使用 class 更改最近的父 div 的 innerHTML 來做這件事有點奇怪。
manifest.json:
{
"name": "Logo Replacer",
"version": "1.0",
"manifest_version": 2,
"web_accessible_resources" : [
"images/*.png"
],
"description": "Replaces Google logo",
"permissions": ["activeTab", "declarativeContent", "storage"],
"content_scripts" : [
{
"matches" : [
"<all_urls>" //normally I want only google.com here but idk how
],
"js": ["changePicture.js"]
}
]
}
更改圖片.js:
var googleLogo = document.getElementsByClassName("doodle"); //the parent div has the classes "logo" and "doodle"
googleLogo.innerHTML = "<a href=\"https://www.google.com\" data-hveid=\"8\"><img alt=\"Alt\" height=\"33\" src=\"https://example.com/logo.png\" title=\"Title\" width=\"92\" border=\"0\" data-atf=\"1\"></a>";
document.getElementsByClassName()
返回一個html collection object
。 對於 select 這個 class 的特定第一個元素,您必須像這樣指定索引 -
var googleLogo = document.getElementsByClassName("doodle")[0];
您還可以使用querySelector
來簡化您的方法來獲取特定的img
,然后像下面這樣更改src
-
document.querySelector(".logo.doodle a img").src = "Your desired source"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.