2015-05-24 101 views
0

我知道這個問題已被問過,但我的代碼仍然無法正常工作,我不知道爲什麼;我試圖做一個Chrome擴展,改變類的profilePic IMG「的圖像與點擊使用jquery更改img的src

這是我到目前爲止的代碼: background.js:

chrome.browserAction.onClicked.addListener(function (tab) { 
    if (tab.url.indexOf("https://www.facebook.com") != -1) { 
     chrome.tabs.executeScript(tab.id, { 
      "file": "contentscript.js" 
     }, function() { 
      console.log("Script Executed"); 
     }); 
    } 
}); 

contentscript.js:

alert("alert"); 
$(document).ready(function() { 
$(document).find('.profilePic img').attr('src', 'second.jpeg')  
}); 

的manifest.json

{ 
    "name": "stuff", 
    "version": "0.0.1", 
    "manifest_version": 2, 

    // Recommended 
    "description": "replaces fb profile pictures", 
    "web_accessible_resources": [ 
    "second.jpeg" 
    ], 
"background":{ 
      "scripts":["background.js"], 
      "page": "background.html" 
     }, 
     "permissions":["https://www.facebook.com/*"], 
    "content_scripts": [ 
     { 
      "matches":["http://www.facebook.com/*"], 
      "js":["jquery-2.1.4.min.js","background.js","contentscript.js"], 
      "all_frames": true, 
      "run_at": "document_start" 
     } 
    ], 
    "browser_action": { 
    "default_icon": "icon.png", 
    "default_title": "testing" 
} 
} 

警報彈出就好了,但沒有什麼改變......我有jquery已經包含在我的清單 編輯:我認爲這是上傳jquery的問題,因爲控制檯日誌錯誤是'$未定義'。抱歉!完全初學者在這裏:(

+2

您在一處提到'contentscript.js',而在另一處提到'content.js'。 – jfriend00

+0

向我們展示整個content.js代碼請 – Zl3n

+0

需要查看HTML。我想我知道這個問題,只需要確定。 –

回答

0

這很可能是因爲profilePic類被用在img標籤上Facebook的個人資料圖片使用類profilePic與選擇器,這可能是什麼讓你絆倒如果它看起來像這樣您目前的形式代碼會工作,它應尋求profilePic類,然後發現其中的任何img標籤,更換src屬性。

<div class="profilePic"> 
<img src="https://www.google.com/images/srpr/logo11w.png"> 
</div> 

但是因爲你有圖像本身,您將會在profilePic類需要重構你的jQuery,所以它看起來像這樣

$(".profilePic").attr("src","second.jpeg"); 

你也可以做這樣的事情,我不完全確定Facebook是如何放在一起的,但是這是從我的頁面中獲取的。

$("#fbPageFinchProfilePic img").attr("src","second.jpeg"); 

您可以使用此測試的jsfiddle了這一點:http://jsfiddle.net/u8zr55vz/1/

我希望幫助!請務必閱讀jQuery .attr()here以獲取更多信息。

+0

不幸的是,這兩個更改都沒有工作:( – imhappi

+0

什麼控制檯日誌說,它應該提供某種錯誤,如果你可以提供其餘的代碼,我們將不勝感激 –

+0

抱歉,我認爲這是加載jquery的問題;它表示'$未定義'。我正在編輯提供所有代碼的問題! – imhappi