谷歌音乐测试版:现在使用Javascript进行播放

时间:2021-01-31 17:03:31

I am working on my first extension for Google Chrome. I want to be able to hit the "Thumbs Up" button on the Google Music Beta page using my extension. For some reason, the thumbs up button seems to be much more complicated than shuffle, repeat, play, next, and previous. For all of those, the following code works:

我正在开发谷歌Chrome的第一个扩展。我希望能够按下谷歌音乐测试页面上的“拇指向上”按钮,使用我的扩展。由于某些原因,竖起大拇指的按钮似乎比洗牌、重复、播放、下一个和上一个要复杂得多。对于所有这些,以下代码是有效的:

chrome.tabs.executeScript(tab_id,
            {
              code: "location.assign('javascript:SJBpost(\"" + command +
                    "\");void 0');",
              allFrames: true
            });

where command="playPause", "nextSong", "prevSong", "toggleShuffle", "togglePlay", etc.

其中command=“playPause”、“nextSong”、“prevSong”、“toggleShuffle”、“togglePlay”等。

I figured a lot of those out using the developer tools to follow the stack trace and see the arguments given to SJBpost. Trying SJBpost with "thumbsUp" returns an error.

我使用开发人员工具跟踪堆栈跟踪并查看给SJBpost的参数,得出了很多结论。使用“thumbsUp”尝试SJBpost会返回一个错误。

Obviously this question is going to be restricted to a smaller crowd since not everyone is going to be able to view the source of Google Music, but if you can help me out, I would greatly appreciate it.

显然,这个问题将被限制在一小部分人,因为不是每个人都能看到谷歌音乐的来源,但如果你能帮助我,我将非常感激。

The div for the thumbs up on the Google Music page looks like this:

在谷歌音乐页面上,拇指向上的div是这样的:

<div id="thumbsUpPlayer" class="thumbsUp" title="Thumbs up"></div>

Now, I've tried doing this using jQuery:

现在,我试着使用jQuery:

$("#thumbsUpPlayer").click()

But I get TypeError, undefined_method message in the javascript console.

但是我在javascript控制台得到了TypeError, undefined_method消息。

Any help would be greatly appreciated. I am a huge beginner to javascript and plugins, and all of this stuff, and I'm really excited to get these last pieces of the extension together.

如有任何帮助,我们将不胜感激。我是javascript和插件以及所有这些东西的初学者,我很高兴能把这些扩展的最后部分整合在一起。

Thank you!

谢谢你!

3 个解决方案

#1


3  

It seems that Google Music Beta doesn't actually listen on the click() event per se, rather, it is based on the events which usually precede the actual click event: mouseover, mousedown and mouseup.

看来谷歌Music Beta实际上并没有监听click()事件本身,而是基于通常在实际单击事件之前的事件:mouseover、mousedown和mouseup。

I'm not a jQuery expert, so I can't exactly figure out why $("#thumbsUpPlayer").mouseover().mousedown().mouseup() doesn't work (neither does doing .trigger).

我不是jQuery专家,所以我不知道为什么$("#thumbsUpPlayer").mouseover().mousedown().mouseup()不工作(也不做。触发器)。

Anyway, here's some (tested on June 21) working javascript code (no dependencies).

总之,这里有一些(在6月21日测试的)可用的javascript代码(没有依赖项)。

function triggerMouseEvent(element, eventname){
    var event = document.createEvent('MouseEvents');
    event.initMouseEvent(eventname, true, true, document.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, element);
    element.dispatchEvent(event);
}

function replicateClick(element){
    triggerMouseEvent(element, 'mouseover');
    triggerMouseEvent(element, 'mousedown');
    triggerMouseEvent(element, 'mouseup');
}

function thumbsUp(){
    replicateClick(document.getElementById('thumbsUpPlayer'));
}

function thumbsDown(){
    replicateClick(document.getElementById('thumbsDownPlayer'));
}

It should be probably fairly easy to use, just call thumbsUp() if you want a thumbs up or call thumbsDown() if you want to thumbs down.

它应该很容易使用,如果你想竖起大拇指,只需调用thumbsUp();如果你想竖起大拇指,可以调用thumbsDown()。

#2


2  

I've been looking into this today and found that you can trigger a lot of the commands using SJBpost from the console.

我今天一直在研究这个问题,发现您可以使用控制台的SJBpost触发许多命令。

http://hints.macworld.com/article.php?story=20110622061755509

http://hints.macworld.com/article.php?story=20110622061755509

SJBpost('thumbsUpPlayer');

Commands I've tried and can confirm working:

我试过的命令,可以确认有效:

  1. playPause
  2. playPause
  3. thumbsUpPlayer
  4. thumbsUpPlayer
  5. thumbsDownPlayer
  6. thumbsDownPlayer
  7. prevSong
  8. prevSong
  9. nextSong
  10. nextSong

I'm still looking into commands which let you retrieve current artist / track info. Let me know if you find out what those are.

我还在研究命令,让你检索当前的艺术家/跟踪信息。如果你知道这些是什么,请告诉我。

#3


0  

The following snippet from the 'music plus for google' chrome extension might be of interest:

“谷歌的音乐+”chrome扩展的以下片段可能会让你感兴趣:

function playback_action(type, callback) {
  var $button;
  if (type == 'playPause') {
    $button = $('button[data-id="play-pause"]');
  }
  else if (type == 'nextSong') {
    $button = $('button[data-id="forward"]');
  }
  else if (type == 'prevSong') {
    $button = $('button[data-id="rewind"]');
  }
  else if (type == 'currently_playing') {
    $button = $('button[data-id="play-pause"]');
  }
  if ($('button[data-id="play-pause"]').attr('disabled')) {
    $instant_mix = $('li[data-type="rd"]').click();
    setTimeout(function() {
      $('div[data-type="im"] .radio-icon').first().click();
    }, 1000);
  }
  else {
    $button.click();
  }
  callback();
}

When the above snippet stops working, check the link again and see if maybe the author updated the extension to something that works.

当上面的代码片段停止工作时,再次检查这个链接,看看作者是否更新了这个扩展到某个有效的东西。

#1


3  

It seems that Google Music Beta doesn't actually listen on the click() event per se, rather, it is based on the events which usually precede the actual click event: mouseover, mousedown and mouseup.

看来谷歌Music Beta实际上并没有监听click()事件本身,而是基于通常在实际单击事件之前的事件:mouseover、mousedown和mouseup。

I'm not a jQuery expert, so I can't exactly figure out why $("#thumbsUpPlayer").mouseover().mousedown().mouseup() doesn't work (neither does doing .trigger).

我不是jQuery专家,所以我不知道为什么$("#thumbsUpPlayer").mouseover().mousedown().mouseup()不工作(也不做。触发器)。

Anyway, here's some (tested on June 21) working javascript code (no dependencies).

总之,这里有一些(在6月21日测试的)可用的javascript代码(没有依赖项)。

function triggerMouseEvent(element, eventname){
    var event = document.createEvent('MouseEvents');
    event.initMouseEvent(eventname, true, true, document.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, element);
    element.dispatchEvent(event);
}

function replicateClick(element){
    triggerMouseEvent(element, 'mouseover');
    triggerMouseEvent(element, 'mousedown');
    triggerMouseEvent(element, 'mouseup');
}

function thumbsUp(){
    replicateClick(document.getElementById('thumbsUpPlayer'));
}

function thumbsDown(){
    replicateClick(document.getElementById('thumbsDownPlayer'));
}

It should be probably fairly easy to use, just call thumbsUp() if you want a thumbs up or call thumbsDown() if you want to thumbs down.

它应该很容易使用,如果你想竖起大拇指,只需调用thumbsUp();如果你想竖起大拇指,可以调用thumbsDown()。

#2


2  

I've been looking into this today and found that you can trigger a lot of the commands using SJBpost from the console.

我今天一直在研究这个问题,发现您可以使用控制台的SJBpost触发许多命令。

http://hints.macworld.com/article.php?story=20110622061755509

http://hints.macworld.com/article.php?story=20110622061755509

SJBpost('thumbsUpPlayer');

Commands I've tried and can confirm working:

我试过的命令,可以确认有效:

  1. playPause
  2. playPause
  3. thumbsUpPlayer
  4. thumbsUpPlayer
  5. thumbsDownPlayer
  6. thumbsDownPlayer
  7. prevSong
  8. prevSong
  9. nextSong
  10. nextSong

I'm still looking into commands which let you retrieve current artist / track info. Let me know if you find out what those are.

我还在研究命令,让你检索当前的艺术家/跟踪信息。如果你知道这些是什么,请告诉我。

#3


0  

The following snippet from the 'music plus for google' chrome extension might be of interest:

“谷歌的音乐+”chrome扩展的以下片段可能会让你感兴趣:

function playback_action(type, callback) {
  var $button;
  if (type == 'playPause') {
    $button = $('button[data-id="play-pause"]');
  }
  else if (type == 'nextSong') {
    $button = $('button[data-id="forward"]');
  }
  else if (type == 'prevSong') {
    $button = $('button[data-id="rewind"]');
  }
  else if (type == 'currently_playing') {
    $button = $('button[data-id="play-pause"]');
  }
  if ($('button[data-id="play-pause"]').attr('disabled')) {
    $instant_mix = $('li[data-type="rd"]').click();
    setTimeout(function() {
      $('div[data-type="im"] .radio-icon').first().click();
    }, 1000);
  }
  else {
    $button.click();
  }
  callback();
}

When the above snippet stops working, check the link again and see if maybe the author updated the extension to something that works.

当上面的代码片段停止工作时,再次检查这个链接,看看作者是否更新了这个扩展到某个有效的东西。