提交时,Chrome扩展程序表单输入文本为空白 [英] Chrome extension form input text is blank on submit

查看:136
本文介绍了提交时,Chrome扩展程序表单输入文本为空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立一个Chrome扩展,从链接下载一堆项目,主要逻辑在我的 download.js 文件中,我希望能够以指定在哪些下载子文件夹中,我想将它们全部捆绑在一起,但输入字段的值似乎为空。这是我迄今的代码

doctype html>
< html>
< head>
< title>下载个人简历< / title>
< script src =popup.js>< / script>
< / head>
< body>
< form id =folderForm>
下载次数:
< input type =textid =folder>
< input type =submitid =downloadvalue =下载CV>
< / form>
< / body>
< / html>



popup.js

 函数bundleItems(){
chrome.tabs.executeScript({
file:'download.js'
});

$ b document.addEventListener('DOMContentLoaded',function(){
var downloadButton = document.getElementById('download');
downloadButton.addEventListener('点击',bundleItems);
});

chrome.extension.onRequest.addListener(function(logs){
var folder = document.getElementById('folder')。value;

logs.map (function(log){
chrome.downloads.download({
url:log.attachment.link,
filename:folder +'/'+ log.attachment.filename
});
});
});

我从 download.js 到 popup.js ,如果我尝试在下载中下载它,一切都会正常工作,所以我觉得发布我的 download.js 文件将无用。但是,如果我尝试显示文件夹变量,它是空的。我在同一个问题上搜索了很多其他的帖子,但没有一个解决方案似乎适用于我。

无法提交到Chrome扩展程序页面。在这种情况下,没有Web服务器来处理您的POST请求。这样做只需重新加载文档(从表单清除值)。

您需要阻止提交,而不是指定 type =button作为你的按钮。


I'm trying to build a chrome extension that downloads a bunch of items from links, the main logic is in my download.js file and I want to be able to specify in which downloads subfolder I'd like to bundle them all but the value of the input field seems to be empty. Here's my code so far

popup.html

<!doctype html>
<html>
  <head>
    <title>Download CVs</title>
    <script src="popup.js"></script>
  </head>
  <body>
    <form id="folderForm">
      Subdir of downloads:
      <input type="text" id="folder">
      <input type="submit" id="download" value="Download CVs">
    </form>
  </body>
</html>

popup.js

function bundleItems() {
  chrome.tabs.executeScript({
    file: 'download.js'
  });
}

document.addEventListener('DOMContentLoaded', function() {
  var downloadButton = document.getElementById('download');
  downloadButton.addEventListener('click', bundleItems);
});

chrome.extension.onRequest.addListener(function(logs) {
  var folder = document.getElementById('folder').value;

  logs.map(function(log) {
    chrome.downloads.download({
      url: log.attachment.link,
      filename: folder + '/' + log.attachment.filename
    });
  });
});

I'm sending information from download.js to popup.js and everything works if I try to download it in downloads, so I feel posting my download.js file will be useless. However, if I try to display the folder variable, it's empty. I've searched for lots of other posts on the same issue and none of the solutions seem to work for me.

解决方案

You cannot submit to a Chrome extension page. There is no web server to process your POST request in this case. Doing so simply reloads the document (clearing your value from the form).

You need to prevent submitting instead, by specifying type="button" for your button.

这篇关于提交时,Chrome扩展程序表单输入文本为空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆