我嘗試使用擴展名爲Chrome的Facebook頁面上的元素添加一個類。不幸的是,我的腳本無法正常工作......我的目標是選擇包含Feed中廣告的帖子。所有的帖子容器看起來都是相似的,唯一的區別是「贊助」標籤,它是主容器的高度子標籤。使用Chrome擴展在HTML頁面中添加類
的script.js:
var x = document.getElementsByClassName("uiStreamSponsoredLink").parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
x.className += "otherclass";
清單:
{
"name": "Project",
"version": "0.0.1",
"manifest_version": 2,
"default_locale": "en",
"icons": {
"16": "img/icon16.png",
"48": "img/icon48.png",
"128": "img/icon128.png"
},
"permissions": [
"*://*.facebook.com/*"
],
"content_scripts": [
{
"matches": [
"*://*.facebook.com/*"
],
"css": [
"content_script/replace-reactions.css"
],
"js": [
"content_script/script.js"
]
}
],
"web_accessible_resources": [
"img/*.png"
]
}
您應該首先檢查瀏覽器控制檯是否有錯誤。 'document.getElementsByClassName(「uiStreamSponsoredLink」)。例如,parentNode'確實是錯誤的,因爲getElementsByClassName返回一個NodeList,所以您需要單獨訪問該列表上的項目。 – CBroe
你的意思是做一個foreach?問題是,當你滾動 – AlphaNico