Chrome扩展程序开发Instagram加载事件

延斯·克维斯特(Jens Kvist)

我最近开始开发一些有趣的次要Chrome扩展程序。我目前正在开发适用于Instagram的“立即下载”扩展程序,该扩展程序使您可以通过单击标题为“获取”的值的下载按钮单击来下载Instagram图像。

正如您在下面的屏幕快照中看到的那样,该按钮按预期显示。

在此处输入图片说明

但是,当我向下滚动它加载新帖子的位置时,该按钮不会出现。

在此处输入图片说明

我现在的问题是,如何预测加载事件,以便该按钮会出现在正在加载的帖子上?

到目前为止,我的jQuery看起来像这样,

jQuery(document).ready(function() {
     jQuery(window).load(function() {
         var location = jQuery('._s6yvg');
         jQuery(location).each(function() {
             var image = jQuery(this).parent().find('div > div > div > div > img').attr('src');
             jQuery('<a class="get-button" href="' + image + '">Get</a>').appendTo(this);
         });
     });
});

If you need more information about the extension just let me know, I think I got the most important written. I am sorry for the bad language. I'm not a native speaker.

Thanks in advance.

Wes Foster

Since there isn't an event for you to try and tie into, you need to create your own "event." In this case, it would be for when images loaded.

Take into consideration what happens when a new set of images are loaded on the page. One thing that happens is the document's height will change. When new content is dynamically loaded, the height of the document will increase. Still with me?

Knowing this, we can create a function that checks the height of the document every interval. I'm not going to write the full script for you, but this below is an example of what you can do.

// Your "append" method
function appendButton()
{
  var location = jQuery('._s6yvg');
  jQuery(location).each(function() {
    var image = jQuery(this).parent().find('div > div > div > div > img').attr('src');
    jQuery('<a class="get-button" href="' + image + '">Get</a>').appendTo(this);
  });
}

// This will monitor the document's height.
// When new images are loaded (thus making the document height increase),
// we will call the `appendButton` method again.
function monitorDocumentHeight()
{
  // Get the current $(document).height()

  // Compare the current height to the most recent height variable

  // If there is a difference in current_height and last_height:
    // > then the height has changed and we should
    // > call the `appendButton` method and store this height value.

  // Set an interval to run this method again and check the height (200ms)
}

jQuery(document).ready(function() {
  appendButton();             // The initial call
  monitorDocumentHeight();    // Start the interval
});

请注意,这还将把按钮附加到现有图像上,这些图像上有一个按钮。您需要通过在appendButton方法中添加条件来避免这种情况

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章