5

Bir krom uzantısı yapıyorum. Bu uzantının bir bölümü, sayfadaki onClick etkinliklerini etkinleştirmek için bir tıklamayı simüle edebilmelidir. İşteSimulated Click to Chrome Extension

function checkForValidUrl(tabId, changeInfo, tab) { 
    // If the letter 'g' is found in the tab's URL... 
    if (tab.url.indexOf('maps') > -1 && tab.url.indexOf('google') > -1) { 
    // ... show the page action. 
    chrome.pageAction.show(tabId); 

    } 
}; 

// Listen for any changes to the URL of any tab. 
chrome.tabs.onUpdated.addListener(checkForValidUrl); 


chrome.pageAction.onClicked.addListener(function() { 
    document.getElementById("paneltoggle2").click(); 
}); 

Ben Chrome'un java script hata ayıklama gelen alıyorum hata mesajı olup:

Error in event handler for 'pageAction.onClicked': Cannot call method 'click' of null TypeError: Cannot call method 'click' of null 
    at chrome-extension://deogcaeekneeagffbhdlflichjlodlem/js/main.js:26:42 
    at chrome.Event.dispatchToListener (event_bindings:387:21) 
    at chrome.Event.dispatch_ (event_bindings:373:27) 
    at dispatchArgs (event_bindings:249:22) 
    at Object.chromeHidden.Event.dispatchEvent (event_bindings:257:7) event_bindings:377 
chrome.Event.dispatch_ event_bindings:377 
dispatchArgs event_bindings:249 
chromeHidden.Event.dispatchEvent event_bindings:257 

Ben izinlerle ilgili bir şey olduğunu tahmin ediyorum Burada arka plan komut dosyasından kodudur manifest dosyasında ... Şu anda sadece "sekmeler" için izinlerim var. Tıklamayı simüle etmek ve bir hata almamak için etkinleştirmem gereken başka izinler var mı? Oh ve bu sürüm 2 manifest protokolü ile yetenekli yapmaya çalışıyorum.

sayesinde Leinardo

cevap

4

Senaryo yürütme ortamları için farklıdır uzantı ve sayfa.

Örneğin, Google arama alanında bazı metni yapıştırmak chrome.tabs.executeScript

Kullanım

Dosya: manifest.json

{ 
    "name": "My Test", 
    "version": "1", 
    "manifest_version": 2, 
    "background": { 
     "scripts": ["background.js"] 
    }, 
    "page_action": { 
     "default_icon": "icon.png" 
    }, 
    "permissions": ["tabs", "http://*/*", "https://*/*"] 
} 

Dosya: background.js

function checkForValidUrl(tabId, changeInfo, tab) { 
    if (tab.url.indexOf("g") > -1) { 
     chrome.pageAction.show(tabId); 
    } 
} 

chrome.tabs.onUpdated.addListener(checkForValidUrl); 

chrome.pageAction.onClicked.addListener(function(tab) { 
    chrome.tabs.executeScript(tab.id, {code: "document.getElementById('gbqfq').value = 'Hello World!';"}); 
}); 

yılında manifest dosyası barındırmak için izin gerekiyor ("http://*/*").

Ama soru kesinlikle JavaScript Tıklama etkinliğini olsaydı, buraya bak How to simulate a click with JavaScript?

+0

mükemmel çalıştı. Çok teşekkür ederim! –

İlgili konular