【问题标题】:How to rewrite twitter images dynamically by jQuery?如何通过 jQuery 动态重写 twitter 图片?
【发布时间】:2019-07-03 08:09:51
【问题描述】:

我是 Javascript 和 jQuery 的初学者,并试图删除 twitter 时间线中的所有图标。

我想要做的是,动态删除 twitter-timeline 中的所有图标(我的意思是,每次按下“查看新推文”按钮时),并且用随机选择的颜色重新编写每个图标。

所以,我要做的就是这样。 https://photos.google.com/share/AF1QipNU-ct8h6oA1_-QNgA4rLNrBpmdbULS-THe3Zt6FONNkxhMwI4rrBT-x0weyRq7YA?key=dDhHaENQNzkzLVYwYm5zUWczTF9XVl9RdFpGYUVB

这是我的代码。

// If class containts 'avatar', just rewrite.

// rewrite for each tag https://symfoware.blog.fc2.com/blog-entry-1515.html

$(function(){
    $("img").each(function(){

      // https://stackoverflow.com/questions/3196613/jquery-determine-if-ul-has-class-or-another-one

        if ($('img').hasClass('Avatar Avatar--size32') ||
           $('img').hasClass('avatar size32') ||
           $('img').hasClass('DashboardProfileCard-avatarImage js-action-profile-avatar') ||
           $('img').hasClass('avatar js-action-profile-avatar ') ||
           $('img').hasClass('top-timeline-tweet-box-user-image avatar size32') ||
           $('img').hasClass('avatar js-action-profile-avatar ') ||
           $('img').hasClass('avatar js-action-profile-avatar') ||
           $('img').hasClass('MomentUserByline-avatar') ||
           $('img').hasClass('ProfileAvatar-image') ||
           $('img').hasClass('ProfileCardMini-avatarImage')
        ) {
          // https://peacepopo.net/blog-entry-161.html
          var hue = 'rgb(' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ',' + (Math.floor(Math.random() * 256)) + ')';
          // Rewrite image to random colors
          // http://shanabrian.com/web/jquery/image01.php

          var stylechar = "background-color:" + hue
           $("img").removeAttr('src');
           $("img").attr('style',stylechar);
      }
    });
});


一些图标被删除了,但是有很多问题。 问题是,

  • 仅移除时间线中 2 个或少数几个图标的顶部。
  • 不是动态工作。当按下“新推文”按钮时,图标会自行显示。
  • icon-backgroundcolor 仍未改变。

如果您知道如何解决此问题,我将不胜感激。 谢谢。

【问题讨论】:

    标签: javascript jquery twitter google-chrome-extension


    【解决方案1】:

    您没有说明您的代码设置为在什么位置执行(您应该将其设置为在清单中记录空闲)。这可能会有所帮助,但您将需要某种事件来处理动态变化的 twitter 内容。 将您的代码放入一个函数中,然后您可以收听“查看新推文”按钮的点击或设置一个 Mutation Observer 以在页面更改时再次执行您的函数。

    【讨论】:

      猜你喜欢
      • 2017-12-22
      • 2017-11-01
      • 2021-10-21
      • 2017-03-13
      • 1970-01-01
      • 2013-11-12
      • 2013-05-02
      • 1970-01-01
      • 2015-03-30
      相关资源
      最近更新 更多