我是使用chrome擴展的新手,並且遇到了設置chrome擴展的問題。我希望擴展從網頁中讀取特定的值,然後從一個新的選項卡中創建的燒瓶應用程序打開一個特定的頁面(具有多個輸入字段的表單),然後使用已被刮掉的值從我的燒瓶應用程序填充頁面中的特定字段。如何製作可打開網頁表單並自動填充特定輸入字段的Chrome擴展程序?
我已經設法得到擴展來生成一個新的選項卡,並從我的燒瓶應用程序加載頁面,但我無法獲得填充字段。看起來頁面在填充字段之前被加載。我粘貼了一些代碼來告訴你我有多遠。另一個問題是我使用executeScripts的代碼參數來執行填充操作,但我似乎無法將參數傳遞到代碼字符串中(我懷疑這不是這樣做的方式,但我正在關閉我已經找到了從這裏https://stackoverflow.com/a/41094570/1977981 這一點任何幫助,將不勝感激非常有幫助的答案。
的manifest.json
{
"manifest_version": 2,
"name": "My Cool Extension",
"version": "0.1",
"permissions": [
"http://localhost:****/lesson/1/note/new/"
],
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": ["jquery-3.2.1.min.js", "content.js"]
}
],
"browser_action": {
"default_icon": "icon.png"
},
"background": {
"scripts": ["jquery-3.2.1.min.js", "background.js"]
}
}
content.js
// Triggered by sendMessage function in background.js
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
// listening for request message
if(request.message === "clicked_browser_action") {
// Retrieve Lesson title from current tab
var lesson = $('._tabbed-sidebar--title--1Dx5w').find('span').text()
// output this value to the console
console.log(lesson);
// Send a single message to the event listener in your extension i.e. background.js
chrome.runtime.sendMessage({"message": "open_new_tab", "lesson": lesson})
}
}
);
background.js
// Called when the user clicks on the browser action icon.
chrome.browserAction.onClicked.addListener(function(tab) {
// Send a message to the active tab
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
// take the current tab shown in the window
var activeTab = tabs[0];
// Send a message to contents.js - this message is being listened for by contents.js and the runtime.onMessage event is fired in content.js script
chrome.tabs.sendMessage(activeTab.id, {"message": "clicked_browser_action"});
});
});
// listening for "open_new_tab" message from content.js
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if(request.message === "open_new_tab") {
// create new tab but do not activate the tab yet
chrome.tabs.create({"url": "http://localhost:5000/lesson/1/note/new/", active: false }, function(tab){
// load jquery functionality execute script
chrome.tabs.executeScript(tab.id, {file: "jquery-3.2.1.min.js"}, function(results){
chrome.tabs.executeScript(tab.id,{code:`
(function(arguments){
var count = 100; //Only try 100 times
function autofill(){
var lesson = $('.lesson_subsection');
console.log(lesson);
if(lesson){
lesson.value = arguments[0].lesson;
} else {
if(count-- > 0){
//The elements we need don't exist yet, wait a bit to try again.
setTimeout(autofill,250);
}
}
}
autofill();
}, request)();
`}, function(results){
chrome.tabs.update(tab.id,{active:true});
}); //END OF second executeScript function
}); // END OF first executeScript function
} // END OF chrome.tabs.create anonymous function
); // END OF chrome.tabs.create
} // END OF if(request.message === "open_new_tab") {
}); // END OF addListener anonymous function
注入的代碼是一個內容腳本,它在不同的頁面上運行,因此它不能請參閱'request'。您需要通過連接JSON.stringify將其明確添加到代碼字符串中,如[使用內容腳本將代碼插入頁面上下文中]中接受答案的末尾所示(// stackoverflow.com/a/9517879) – wOxxOm
另外,'},request)();'似乎是一個錯字。你可能是指'})(request);' - 但看到我上面的評論。 – wOxxOm