[英]How to send POST request from chrome extension (without clicking) to Flask when browser is reloading the page?
我想合並下面的代碼段,以便能夠在Chrome加載頁面時將Chrome擴展程序中的發布請求(包含URL)發送到Flask,而無需單擊擴展程序圖標。 這可能嗎? 此外,我希望僅在我聲明的特定頁面上顯示彈出窗口(我相信manifest.json中有一種方法(“匹配項”),但是,我不知道該如何實現。)
chrome.tabs.query({
active: true,
lastFocusedWindow: true
}, function(tabs) {
var tab=tabs[0];
console.log(tab.url);
var xhr = new XMLHttpRequest();
xhr.addEventListener("readystatechange", function () {
if (xhr.readyState == 4) {
console.log(xhr.responseText);
alert(xhr.responseText)
}
});
xhr.open("POST", "http://localhost:5000/",true);
xhr.send(tab.url);
});
該腳本允許我在點擊時發送帖子請求,但是我需要點擊后發送。 我還發現了這樣一個腳本,該腳本在右下角顯示有關瀏覽器內容更改的所有信息:
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
alert(changeInfo.url);
console.log(changeInfo.url);
我試圖合並這兩個,但沒有結果。 我是JS和Chrome擴展程序的新手,因此,感謝您的幫助。
在達到這一點之后,我希望能夠有條件地顯示彈出窗口,這僅在特定頁面將被加載時進行,因此,我感謝您的進一步提示。
您可以使用chrome.webNavigation.onCommitted並指定要監視的URL列表。
下面的代碼使用console.log,因此輸出顯示在后台控制台中 。
manifest.json的:
{
"name": "test",
"version": "0.0.1",
"manifest_version": 2,
"background": {
"scripts": [
"background.js"
],
"persistent": false
},
"permissions": [
"webNavigation",
"http://localhost/"
]
}
background.js:
chrome.webNavigation.onCommitted.addListener(onCommitted, {
url: [
{hostEquals: 'www.example.org'},
{urlPrefix: 'https://example.org/'},
{urlMatches: '^https://(www\\.)?example.org/.*$'},
],
});
function onCommitted(info) {
const xhr = new XMLHttpRequest();
xhr.onload = () => {
console.log('%d:%d', info.tabId, info.frameId, info.url, xhr.responseText);
};
xhr.open('POST', 'http://localhost:5000/');
xhr.send(info.url);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.