0

I'm trying to clone the window. I used lodash for this. Lodash didn't work, because it brought me empty object. I've tried the methods on this link. None of them worked. How do I clone the window?

let w=_.cloneDeep(window);// w={}

//test
console.log(window.console===w.console);
Efe Zeybek
  • 83
  • 1
  • 2
  • 14
  • has there been done some modification to the `window` or is it still in pristine "condition"? – humanityANDpeace Aug 25 '19 at 07:50
  • @humanityANDpeace I don't know if any changes have been made. I'm using this in the tampermonkey and the site deletes window functions. for example, console.log does not work. So I'm trying to clone the window. – Efe Zeybek Aug 25 '19 at 07:58
  • I was once interested to "sandbox" (see https://stackoverflow.com/q/53666880/1711186) some javascript, by removing some functions. Basically you can get another -potentially "uncircumcised" `window`- via inserting an iframe, which would maybe help you out to have console.log as well? – humanityANDpeace Aug 25 '19 at 08:11
  • sorry the more relevant question was this one [Assuming I deleted window.alert, how would I get it back?](https://stackoverflow.com/q/48050777/1711186) – humanityANDpeace Aug 25 '19 at 08:20
  • I think it wouldn't be a generic solution. Because dispatchEvent doesn't work and there may be other non-functioning functions that I don't know about. My idea is to clone the window into a variable and then clone it into the window. – Efe Zeybek Aug 25 '19 at 08:25
  • I see :) well I hope you will receive a solution/answer, which I cannot really provide given I do not use tampermonkey and have little insight what it does to provoke your initial problem. I have a feeling that cloning the global object is a challange given that whatever variable you clone it to, has a potential to end up being accessible via the very global object `window` hence it might be a auto/self-reference-circle problem. At best I would do the cloning in the context of a function, as to avoid cloning window to a variable that is part inside of the global `window` itself. good luck! – humanityANDpeace Aug 25 '19 at 08:58

1 Answers1

0

Hi :) I found the solution. You should use cloneDeepWith.

const customizer = (value) => {
 return value
}

let w = _.cloneDeepwith(window, customizer)

console.log(window.console === w.console) // will true
Tenny Kim
  • 1
  • 1