How to overwrite a Polymer content scripts function

60 Views Asked by At

I been trying to understand what part of code on youtube.com does redirect you to another site when clicking on a link in the description. For that I have tried to prevent all events to run in relation to the clicked element, but that does not help:

Object.keys(window).filter(
  k => /(?=^on)(?!.*move)(?!.*pointerup)(?!.*auxclick)/.test(k)
).forEach(key => {
  et.addEventListener(key.slice(2), ex => {
    console.log(`stop: ${key}`)
    ex.stopPropagation()
    ex.preventDefault()
  })
})

So I went to the browser console and checked which function is called when the button is pressed. However, when I try to call this function from the debugger, I get `x is not defined'.

Can someone explain why these functions are not accessible, and it's not possible to overwrite them, even from an extension code using a content script?

1

There are 1 best solutions below

2
The Bomb Squad On

So, you can hijack the addEventListener function itself, you can find its source from window.__proto__.__proto__.__proto__ which contains addEventListener, removeEventListener and dispatchEvent

Paste the following code in a youtube tab's inspect element console; no event listeners would be added, neither would any of the globals you referenced be useful and the tab that this code would open, while technically youtube would be VERY BROKEN

EDIT: addEventListener is modified to do default behaviour for everything except click events

function execute(){
  const eventClass=window.__proto__.__proto__.__proto__
  const realListener=eventClass.addEventListener
  eventClass.addEventListener=function(){
    if(arguments[0]!=="click")  realListener.bind(this)(...arguments);
  }

  //now for modification of your code
  Object.keys(window).filter(
    k => /(?=^on)(?!.*move)(?!.*pointerup)(?!.*auxclick)/.test(k)
  ).forEach(function(key){
    const event=key.substr(2)
    realListener.bind(window)(key.slice(2), ex => {
      console.log(`stop: ${key}`)
      ex.stopPropagation()
      ex.preventDefault()
    })
    let dummy=null
    Object.defineProperty(window,key,{get:_=>dummy,set:thing=>dummy=thing})
  })
}
let myWindow=window.open(location.href)
myWindow.eval(`(${execute.toString()})()`)