How to remove DIV based on inner SPAN in tampermonkey

117 Views Asked by At

noob here, I want to hide the entire div using Tampermonkey when it contains "Promoted" under span,

<div>
  <div class="relative">
  <div class="ember-view  occludable-update ">

  <span aria-hidden="true">Promoted</span>
  <span class="visually-hidden">Promoted</span>
 </div>
 </div>
 </div>

I did try based on my finding as below but didn't get it working, any help would be a greatly help, many thanks

// ==UserScript==
// @name         LinkedIn Ad block
// @namespace    http://tampermonkey.net/
// @version      0.4
// @description  I always want my LinkedIn feed in chronological order!
// @include      https://linkedin.com/*
// @include      https://www.linkedin.com/*
// @exclude      https://www.linkedin.com/tscp-serving/*
// @grant        none
// @run-at       document-idle
// @require      https://gist.githubusercontent.com/BrockA/2625891/raw/9c97aa67ff9c5d56be34a55ad6c18a314e5eb548/waitForKeyElements.js
// ==/UserScript==

('div.relative.ember-view:has(span:contains("Promoted"))').remove();
1

There are 1 best solutions below

0
erosman On

Here is a sample code that you can work on and update to suit your requirement:

// ==UserScript==
// @name         LinkedIn Ad block
// @namespace    http://tampermonkey.net/
// @version      0.4
// @description  I always want my LinkedIn feed in chronological order!
// @include      https://linkedin.com/*
// @include      https://www.linkedin.com/*
// @exclude      https://www.linkedin.com/tscp-serving/*
// @grant        none
// @run-at       document-idle
// @require      https://gist.githubusercontent.com/BrockA/2625891/raw/9c97aa67ff9c5d56be34a55ad6c18a314e5eb548/waitForKeyElements.js
// ==/UserScript==

// find all the span
document.querySelectorAll('div.relative div.ember-view span').forEach(item => {
  // check if it includes Promoted (case-sensitive)
  if (item.textContent.includes('Promoted')) {
    // remove the container div
    item.parentElement.parentElement.parentElement.remove();
  }
});