0

I am working on a Chrome Extension that will pop-up a modal with an iframe inside. I worked on it while using google.com as my test page and things worked okay. Then when I tried it out on other sites (espn.com, yahoo.com, etc../, it seems to break all the styling.

Is there another method I can use to add the button to the page which will launch the modal?

manifest.json -

{
"name" : "test",
"version": "0.0.1",
"icons": { "128": "icon32.png" },
"browser_action": {
  "default_icon": "icon.png"
},
"manifest_version": 2,
"description" : "Pop up a window!",
"content_scripts" : [
    {
        "js" : ["init.js"],
        "css": ["styles.css"],
        "matches" : ["http://*/*", "https://*/*"]
    }
]}

init.js -

(function () {
  function fnAddButtons() {
 
  document.body.insertAdjacentHTML("afterbegin", "<button id='modal-btn'>Pop</button>");
  document.body.insertAdjacentHTML("beforeend", "<div id='my-modal' class='modal'><div id='modal-content'><div class='close'></div><div id='modal-body'><iframe src='https://nyc.com' width='100%' height='100%'></iframe></div></div></div>");

  }

  fnAddButtons();


// Get DOM Elements
var modal = document.querySelector('#my-modal');
var modalBtn = document.querySelector('#modal-btn');
var closeBtn = document.querySelector('.close');

// Events
modalBtn.addEventListener('click', openModal);
closeBtn.addEventListener('click', closeModal);
window.addEventListener('click', outsideClick);

// Open
function openModal() {
  modal.style.display = 'block';
}

// Close
function closeModal() {
  modal.style.display = 'none';
}

// Close If Outside Click
function outsideClick(e) {
  if (e.target == modal) {
    modal.style.display = 'none';
  }
}

})();

styles.css -

#search-mm-btn {
    background: #dc3831;
    color: white;
}
:root {
    --modal-duration: 1s;
    --modal-color: #bd302b;
  }
  
  body {
    font-family: Arial, Helvetica, sans-serif;
    background: #f4f4f4;
    font-size: 17px;
    line-height: 1.6;
    display: flex;
    height: 100vh;
    align-items: center;
    justify-content: center;
  }
  
  #modal-btn {
    background: #dc3831;;
    color: #fff;
    border: 0;
    border-radius: 5px;
    cursor: pointer;
    padding: 5px 20px;
    height: 36px;
    width: 100px;
    font-family: arial,sans-serif;
    font-size: 14px;
    position: -webkit-sticky;
    position: sticky;
    top:10px;
    left:10px;
    z-index: 10000000000;
  }
  
  #modal-btn:hover {
    background: #a52924;
  }
  
  .modal {
    display: none;
    position: fixed;
    z-index: 1000000000000001;
    left: 0;
    top: 0;
    height: 100%;
    width: 100%;
    overflow: hidden;
    background-color: rgba(0, 0, 0, 0.5);
    
  }
  
  #modal-content {
    margin: 100px auto;
    width: 60%;
    height: 80%;
    box-shadow: 0 5px 8px 0 rgba(0, 0, 0, 0.2), 0 7px 20px 0 rgba(0, 0, 0, 0.17);
    animation-name: modalopen;
    animation-duration: var(--modal-duration);
  }
  
  #modal-body {
    padding: 0 5px 0 0;
    height:100%;
    background: #fff;
  }

  .close {
    color: #ccc;
    float: right;
    font-size: 30px;
    color: #fff;
  }
  
  .close:hover,
  .close:focus {
    color: #000;
    text-decoration: none;
    cursor: pointer;
  }
  
  @keyframes modalopen {
    from {
      opacity: 0;
    }
    to {
      opacity: 1;
    }
  }
  

yahoo all wacked out

banjax
  • 37
  • 5
  • Use ShadowDOM as explained in your previous duplicate of this question and in the linked topic. – wOxxOm Dec 15 '20 at 22:03
  • My original post was flagged as closed. You gave me the answer to that, but I'd have liked to keep it open for additional comments. While this is a duplicate of my original, the post that claims my question is answered is not helpful. – banjax Dec 15 '20 at 23:50
  • Is there a more authoritative source on using ShadowDOM? – banjax Dec 15 '20 at 23:51
  • There's ton of info on ShadowDOM so choose any source you deem "authoritative". The topic I've linked contains several answers and any of them can solve the problem, AFAICT. – wOxxOm Dec 16 '20 at 08:44

0 Answers0