3

since my Chrome Extension didn't work under Mac I broke the code down and found the problem:

background.js

chrome.browserAction.onClicked.addListener(function(tab){
    alert("Goin to");
    chrome.tabs.executeScript(tab.id, {file: "script.js"});
});

script.js

$('#Hoster_30').trigger('click');
alert("Clicked");

jQuery is loaded and both alerts are shown, but the click event isn't triggered. It works fine when I directly enter the trigger in the js console on the page and under Windows it also works in the add on.

Here my manifest.json

{
    "name": "MacAddon",
    "version": "0.1",
    "permissions": ["tabs", "http://*/", "https://*/"],
    "background": {
        "scripts": ["background.js"]
    },  
    "browser_action": {
        "default_title": "MacAddon"
    },
    "content_scripts": [
    {
      "matches": ["http://*.kinox.to/*", "http://*.streamcloud.eu/*"],
      "js": ["jquery.js"]
    }
    ]
}
Fredrik Sundmyhr
  • 801
  • 6
  • 16
Gumble
  • 145
  • 1
  • 2
  • 9
  • You should have edited your previous question, to include the code (not create a new one). But good job, you've now created a demo to reproduce the problem. That's what we, SO users, like to see, – Rob W Jul 29 '12 at 14:22
  • On which page are you testing the extension? Neither `http://kinox.to/` nor `http://streamcloud.eu/` have an element with ID `Hoster_30`. – Rob W Jul 29 '12 at 14:26
  • http://www.kinox.to/Stream/Die_Simpsons.html every movie and serial has a Hoster_30 – Gumble Jul 29 '12 at 14:31
  • `$('#Hoster_30').data('events').click[0].handler` showed me that the following code is executed: ` getPlayerByMirror(this,'Auto');`. Is the problem solved if you directly call this function, with `this` being the element? Since the function is defined in the page, you have to inject the script to test it. See http://stackoverflow.com/q/9602022?chrome-extension-retrieving-gmails-original-message – Rob W Jul 29 '12 at 14:41
  • No, this doesn't work, but `getPlayerByMirror($('#Hoster_30'), 'Auto')` worked. Thanks, I'll use that for now but If anybody has an Idea why the click Event only worked under Windows would be nice – Gumble Jul 29 '12 at 15:13
  • That was exactely what I suggested. Which Chrome and OSX version are you using? – Rob W Jul 29 '12 at 22:07
  • I am using 10.6.8 and was using Chrome 18. The jQuery.trigger works from Chrome 20+ ! – Gumble Aug 04 '12 at 19:35

1 Answers1

1

For Chrome 19-, you have to inject the code and use the website's own method to switch the Episode.

var s = document.createElement('script');
s.textContent = "getPlayerByMirror($('#Hoster_30'), 'Auto');";
s.onload = function() {
    this.parentNode.removeChild(this);
};
document.head.appendChild(s);

(To find out which function had to be called, I inspected the $().data('events') object in the console: $('#Hoster_30').data('events').click[0].handler).
In Chrome 20+, using .trigger('click') from the content script worked fine:

$('#Hoster_30').trigger('click');

Thanks Rob W. for welcoming me so greatly here and answering the Question!

Community
  • 1
  • 1
Gumble
  • 145
  • 1
  • 2
  • 9