2

I'm working on facebook script that bans everyone who likes post in group.

My code

var links = document.getElementsByClassName("UFINoWrap");
var hrefAttr=[];
var profileID=[];
var elements = document.getElementsByClassName('UFILikeSentenceText');

var buttonID = 0;

function openBanList(id)
{   
    var linkString = "https://m.facebook.com/browse/likes/?id=";
    linkString+=profileID[id];
    var win = window.open(linkString);
}

for (var i = 0; i < elements.length; i++) 
{
    var buttonStringHTML = "      <button onclick=\"openBanList(id)\" id=\"";
    buttonStringHTML+=buttonID;
    buttonStringHTML+="\">Ban.</button>";
    buttonID++;
    elements[i].innerHTML+=buttonStringHTML;
}

for(var i=0; i<links.length; i++) 
{
    hrefAttr.push(links[i].getAttribute("href"));
    var begin = (hrefAttr[i].indexOf("&id") +4);
    profileID.push(hrefAttr[i].substring(begin, hrefAttr[i].length));
}

Console is saying that openBanList is not defined.

Lotherad
  • 115
  • 8
  • 1
    There must be something wrong with the code that you are not showing here. – mic4ael Oct 30 '15 at 13:11
  • 1
    show your complete code specially inside of function a(){ //We need this } – Shailendra Sharma Oct 30 '15 at 13:12
  • Maybe instead of down voting my question just help me? Its my first post, ffs... – Lotherad Oct 30 '15 at 13:28
  • 1
    Possible duplicate of [onClick within Chrome Extension not working](http://stackoverflow.com/questions/13591983/onclick-within-chrome-extension-not-working) – wOxxOm Oct 30 '15 at 13:29
  • @wOxxOm No, that's a wrong dupe target. This is content script code - so inline code is okay, but it executes in a different context. TAA's answer nails it. – Xan Oct 30 '15 at 18:56
  • @Lotherad Please accept my answer if it fully and correctly answered your question. Otherwise, please explain what is wrong with it so me or someone else in the community can answer it better for you. – Marc Guiselin Jan 22 '17 at 07:27

1 Answers1

3

the page and your content script are separate things. content scripts are not injected into the page! Rather, content scripts run alongside the page. The page cannot access the content script, and the content script can only access the page's dom(and your background page).

When you add an onclick event through the dom, and you click on it, the dom looks for a function which was never defined by the part of the page it can access. If you want this to work you are going to have to run this on every one of your buttons:

mybuttonelement.onclick=function(){openbanlistthing(this.id)}
Marc Guiselin
  • 3,064
  • 2
  • 22
  • 36
  • now it works but it doesnt send the ID I just added elements[i].onclick=function(){openBanList(buttonID)}; to the first for – Lotherad Oct 31 '15 at 21:56
  • keep `this.id`. when the button is clicked, `this` will reference the button, so `openBanList(this.id)` should execute openBanList() with the pressed button's id. – Marc Guiselin Nov 01 '15 at 23:29
  • how am I supposed to do that when my button is added by innerHTML? doesnt do anything in console – Lotherad Nov 02 '15 at 13:06
  • **you cant use a dom event!** after your button is created, add the event via javascript like so: http://jsfiddle.net/sc8Lo7ro/ – Marc Guiselin Nov 03 '15 at 14:00
  • Oh, now I understand, thank you, it works. But the next problem is that I'm opening new tab and want to add button in new tab but console is saying that "Cannot set property 'innerHTML' of undefined". I guess it still focuses first tab. Even when I do var win = window.open(blabla); and then win.document.getElementsByClassName(blabla); it is still the same. – Lotherad Nov 04 '15 at 17:00
  • that is a different problem all together. ask a new question and select my answer if you see it fit. – Marc Guiselin Nov 04 '15 at 17:33