Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
197 views
in Technique[技术] by (71.8m points)

Chrome extension : Create new tab and attach a listener

I would like to create an extension that when I click the extension icon opens a list of websites, then when I click of an item of the list a new tab is open with the selected website. I was able to create that scenario, but I would like to full fill some fields of the opened website in the new tab. Does it possible?

You will see in my code that I tried to open an alert on the new opened website, but nothing

Any help would be very much appreciated.

Best Regards, Jer?me.

My script :

From manifest :

"browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html",
    "default_title": "Click here!"
  },

from popup.html

<html>
  <head>
    <title></title>
    <style>
    body {
    font-family: "Segoe UI", "Lucida Grande", Tahoma, sans-serif;
    font-size: 100%;
    }
    .dropdown {
    position: relative;
    display: inline-block;
    }
    .dropdown-content {
    display: block;
    position: absolute;
    background-color: #f1f1f1;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;
    }
    .dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
    }
    .dropdown-content a:hover {background-color: #ddd;}
    .dropdown:hover .dropdown-content {display: block;}
  

    </style>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="popup.js"></script>
  </head>
  <body>
    <div class="dropdown" style="width: 150px;">
      <div class="dropdown-content">
        <a href="http://www.website1.fr" name="website1" class="link">website1</a>
        <a href="http://www.website2.fr" name="website2" class="link">website2</a>
        <a href="http://www.website3.fr" name="website3" class="link">website3</a>
      </div>
    </div>
  </body>
</html>

from popup.js

$(function() {
    $('.link').click(function(){

        var href = $(this).attr('href');
        var websitename = $(this).attr('name');
        
        // Open a new tab with the selected website.
        chrome.tabs.create({url:href,active: true}, function(tab){
            
                alert("Alert on the opened website");
                        
        });

    });
});
question from:https://stackoverflow.com/questions/65945151/chrome-extension-create-new-tab-and-attach-a-listener

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...