通过Chrome扩展程序点击页面上的按钮

时间:2019-08-14 15:11:56

标签: javascript google-chrome google-chrome-extension

我正准备编写Chrome扩展程序来自动完成我的工作, 我试图单击我的扩展名上的一个按钮,然后让脚本单击网站上的3个按钮,其中一个作为类或ID,名称为:“ activityFeedExpandCollapseIcon” 由于某些原因导致其无法正常工作,希望获得帮助。

我已经尝试在论坛上进行搜索了。

popup.Html:

<!doctype html> 
<style>
h1{
text-align:middle;
align:middle;
}
button{
background-color:white;
height:50px;
width:100px;
font-family:david;
font-style:bold;
font-size:16px;
font-weight:300;
}

.body{
 width:120px;
}
</style> 
<html>  
    <head><title>HelpDesk SaleForce Helper</title></head>  
<body>
 <div class="body">
    <div class="right">
    <h1>Change Type</h1>
    <button id="Incident">Change to Incident </button>
    <button id="request">Change to Request </button>
    </div>
    <div class="left">
    <h1>Foward</h1>
    <button id="B7"> Forward to B7 </button>  
    <button id="Sdom">Forward to Sdom </button>
    <button id="Rotem">Forward to Rotem </button>   
    <button id="NH">Forward to NH </button>
    <button id="TLV">Forward to TLV </button>
    </div>
    <script src="popup.js"></script> 
</div>
</body>
</html>  

Popup.js

function injectTheScript() {
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
        // query the active tab, which will be only one tab
        //and inject the script in it
        chrome.tabs.executeScript(tabs[0].id, {file: "content_script.js"});
    });
}

document.getElementById('request').addEventListener('click', injectTheScript);

Content_script.js

function clickrequest() {
    var request = document.getElementsByClassName("activityFeedExpandCollapseIcon");


    request.click();
}

clickrequest();

Manfiest

{
    "manifest_version": 2,
    "name": "Help Desk SalesForce Helper",  
    "description": "Wow",  
    "version": "1.0",    
    "permissions": [    
    "<all_urls>",
    "tabs",
    "activeTab",
    "*://*/*",
    "https://icl--bmcservicedesk.eu14.visual.force.com/*"
    ] ,

    "browser_action": { 
        "default_icon": {
            "19": "images/icons/19.png",
            "38": "images/icons/38.png"
        },
        "default_popup": "popup.html"  
    },

    "icons": {
        "16": "images/icons/16.png",
        "19": "images/icons/19.png",
        "38": "images/icons/38.png",
        "64": "images/icons/64.png",
        "128": "images/icons/128.png"
    }
}

1 个答案:

答案 0 :(得分:0)

找到答案:

 var theButton = document.querySelector('.sidebarShowButton'); //a shorter way to select the first element of a class if (theButton) { // if the element is present in this context, click on it. theButton.click(); } 

和pop.js中的allFrames:false