0

how to make this localStorage.setItem and localStorage.getItem work cross-domain

if (location.href=="https://cs.money"){
localStorage.setItem('items', document.querySelector("#offer_container_bot > div.items > div > div.p").innerText);
var fasi = localStorage.getItem('items')
};


if (location.href=="https://tynachofinder.ge/finder/"){
document.querySelector("body > header > a").innerHTML = (
   localStorage.getItem('items');
)};
d4vit
  • 21
  • 3
  • Does this answer your question? [cross domain localstorage with javascript](https://stackoverflow.com/questions/33957477/cross-domain-localstorage-with-javascript) – VLAZ Apr 09 '20 at 14:52
  • this seems like a major security risk for browser allowing that...but i thought that was blocked – Ctznkane525 Apr 09 '20 at 14:53
  • the security model of browsers explicitly rules out doing this. If you need to store data cross-domain (without using frames), the only way you'll be able to do it is to store it in a server-side database and use session and users. – Christoph Burschka Apr 09 '20 at 14:56

1 Answers1

0

You can't per se. localStorage is very much designed to be same-origin only.

You'd need to implement some other form of cross-origin communication such as tynachofinder.ge including an <iframe> which loads a page from cs.money and then the page in the <iframe> doing something like:

const items = localStorage.getItem('items');
top.postMessage(items, 'https://tynachofinder.ge');

with a matching

window.addEventListener("message", receiveMessage, false);

function receiveMessage(event) {
    if (event.origin === "https://cs.money")
        console.log(event.data);
    }
}

on tynachofinder.ge

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • i am making extension so that is the problem i want to get content from cs.money and use it in tynachofinder.ge – d4vit Apr 09 '20 at 15:22