2015-05-24 98 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