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
371 views
in Technique[技术] by (71.8m points)

javascript - Get HTML content of the active tab with Chrome Extension

I have to make a Chrome Extension : The user can type a ID, CLASS or any CSS selector on the text input, and then, clicking on the button, display the HTML content of the selector of the active html page. Schema

I put my click function on my popup.js file.

BUT, i can't interact with the content.js (content_script), the only js file allowed to interact with the DOM of the current tab.

I can sending message between popup.js and background.js, but not between popup.js and content.js.

My manifest file :

{
"name": "TEST 2 Extension",
"description": "TEST DOM",

"version": "2.0",
"manifest_version": 2,

"content_scripts": [
    {
        "matches": ["http://*/*", "https://*/*"],
        "js": ["content.js"]
    }
],

"background": {
    "scripts": [
        "background.js"
    ]
},

"browser_action": {
  "default_popup": "popup.html"
 },

 "permissions": [
    "activeTab",
    "tabs"
 ]

}

My popup.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Test</title>
</head>
<body>
    <h1>Extension</h1>
    <input id="id" placeholder="Balise id" type="text">
    <button id="btn">Check</button>
    <p>Résultat : <span id="result"></span></p>

    <script src="jquery.js"></script>
    <script src="popup.js"></script>

    </body>
</html>

And, By caricaturing, the popup.js :

$('#btn').on('click', function(){
   var request = $('#id').val();
   
   // SENDING request TO content.js => receive response from content.js :
   $('#result').html(response);
});

And the content.js :

function getElement(request){

    return response = $(request).html();
}

Thanks a lot!

question from:https://stackoverflow.com/questions/65829481/get-html-content-of-the-active-tab-with-chrome-extension

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
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

...