简体   繁体   English

浏览器重新加载页面时,如何从chrome扩展程序(无需单击)向flask发送POST请求?

[英]How to send POST request from chrome extension (without clicking) to Flask when browser is reloading the page?

I want to merge the snippets below to be able to send post request (containing URL) from chrome extension to flask whenever the page in Chrome is loading without clicking the extension' icon. 我想合并下面的代码段,以便能够在Chrome加载页面时将Chrome扩展程序中的发布请求(包含URL)发送到Flask,而无需单击扩展程序图标。 Is this possible? 这可能吗? Moreover I would like the popup to be shown only on specific pages that I declare (I believe there is a way ('matches') for this in manifest.json however, I don't know how to implement this.) 此外,我希望仅在我声明的特定页面上显示弹出窗口(我相信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); 
});

This script allows me to send post request on click, however I need to do this without clicking. 该脚本允许我在点击时发送帖子请求,但是我需要点击后发送。 I found also such a script that displays all information about change in browser content in down-right corner: 我还发现了这样一个脚本,该脚本在右下角显示有关浏览器内容更改的所有信息:

 chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
 alert(changeInfo.url);
 console.log(changeInfo.url);

I tried to merge these two, but with no result. 我试图合并这两个,但没有结果。 I'm kind of newbie to JS and Chrome Extensions, so I would be duty grateful for your help. 我是JS和Chrome扩展程序的新手,因此,感谢您的帮助。

After reaching this point I would like to be able to show popup conditionally, this is only when the specific page will be loaded, so I would appreciate your further hints. 在达到这一点之后,我希望能够有条件地显示弹出窗口,这仅在特定页面将被加载时进行,因此,我感谢您的进一步提示。

You can use chrome.webNavigation.onCommitted and specify a list of URLs to monitor. 您可以使用chrome.webNavigation.onCommitted并指定要监视的URL列表。
The code below uses console.log so the output is shown in the background console . 下面的代码使用console.log,因此输出显示在后台控制台中

manifest.json: manifest.json的:

{
  "name": "test",
  "version": "0.0.1",
  "manifest_version": 2,
  "background": {
    "scripts": [
      "background.js"
    ],
    "persistent": false
  },
  "permissions": [
    "webNavigation",
    "http://localhost/"
  ]
}

background.js: 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.

相关问题 从Chrome扩展程序发送POST请求 - Send POST request from Chrome Extension 如何在没有 JQUERY 的情况下从浏览器扩展向 localhost 发出 POST 请求? - How to do a POST request from a browser extension to localhost WITHOUT JQUERY? 如何在不重新加载的情况下在不同的浏览器 window 中以 chrome 打开当前页面? - How to open the current page in chrome in a different browser window without reloading it? 允许表单发送 POST 请求但阻止页面重新加载 - Javascript - Allow form to send POST request but prevent page from reloading - Javascript Nodemon应用程序崩溃-当我从rest发送发帖请求时 - Nodemon app crashed - when i send a post request from restEasy chrome extension 无法从 Chrome 扩展程序向 REST API 发送 POST 请求 - Can't send POST request to a REST API from a Chrome extension 如何从 Firefox 扩展发送 POST 请求? - How to send a POST request from a Firefox extension? 来自Chrome扩展程序的POST请求 - POST Request from Chrome Extension Chrome扩展程序可以更新Facebook组页面而无需重新加载页面 - Chrome extension that would update facebook group page without reloading page 通过chrome扩展程序中的post request发送URL onload - send the URL onload by post request in chrome extension
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM