2

我需要保存当前加载的音频标签源文件。听起来很简单,但这里有一个问题:源在每个请求中都会提供一个随机声音文件。

音频标签被创建,源集和使用 JavaScript 播放的音频,如下所示:

function createAudio() {
    var audio = document.createElement('audio');

    audio.setAttribute('id', 'file_audio')
    audio.setAttribute('controls', 'controls');
    audio.setAttribute('autoplay', 'true');
    audio.setAttribute('hidden', 'true');
    audio.appendChild(createSource());
    return audio;
}

function createSource() {
    var source = document.createElement('source');
    var d = new Date();
    source.setAttribute('id', 'file_audio_source')
    source.setAttribute('src', 'file.wav?r=' + d.getTime());
    source.setAttribute('type', 'audio/wav');
    return source;
}

this.switchAudio = function() {
    var d = new Date();
    $svjq("#file_audio").find('audio').remove();
    $svjq("#file_audio").find('source').remove();
    $svjq("#file_audio").find('embed').remove();
    if (Modernizr.audio.wav) {
        document.getElementById("file_audio").appendChild(createAudio());
    } else {
        $svjq("#file_audio").append('<embed id="file_audio_embed" name="file_audio_embed" src="file.wav?r=' + d.getTime() + '" autostart="true" cache="false" type="audio/wav" hidden="true" loop="false" enablejavascript="true">');
    }

};

this.playAgain = function() {
    if (Modernizr.audio.wav) {
        document.getElementById('file_audio').play();
    } else {
        document.getElementById('file_audio_embed').play();
    }
};

我需要能够将当前加载的文件保存在源代码中。但是,如果您在浏览器中访问文件 URL,它会返回一个不同的文件。

Watir-WebDriver、Capybara (Capybara-Webkit) 和 Mechanize 等自动化进程也会返回一个随机文件。例如:

require 'capybara'
session = Capybara::Session.new(:selenium)
session.visit('url')
session.click_link 'play sound' #on every click u get a new sound
session.click_link 'play again'

#file_audio_source
e = session.find_by_id('file_audio_source')
e[:src]

#save the current open page and opens it
#session.save_and_open_page

#returns different file
session.visit(e[:src])

#returns different file
session.execute_script("window.open('"+e[:src]+"')")

require 'Mechanize' 
agent = Mechanize.new{|agent| agent.ssl_version, agent.verify_mode = 'SSLv3', OpenSSL::SSL::VERIFY_NONE}

filedata = agent.get(e[:src]).content
aFile = File.new("/Users/me/Documents/test/test111.wav", 'wb')
#aFile.syswrite(filedata)

该文件是否可以嵌入到 HTML 中或缓存?有没有办法获取文件并将其保存在本地?

其他选项包括从声音设备录制或使用麦克风录制播放的声音,尽管此选项并不理想。

4

1 回答 1

1

选项 1:

require 'capybara'
session = Capybara::Session.new(:selenium)
session.visit('url')
session.click_link 'Play sound'#this gets the file into the cache, then use the codes to get in out

选项 2

#execute the javascript that loads the file/creates the sound url. no playing of the sound
session.execute_script("document.getElementById('file_audio').appendChild(createSource());")
e = session.find_by_id("file_audio_source")
session.visit(e[:src])

Watir 和 Capybara 表现出色!:) 但现在的问题是让它无头,似乎无头浏览器的行为与非无头浏览器不同?

一种提供无头功能的方法

def headless_get_file url
  require 'uri'
  res = @session.driver.cookies
  agent = Mechanize.new {|agent| agent.ssl_version, agent.verify_mode = 'SSLv3', OpenSSL::SSL::VERIFY_NONE}

  uri = URI('https://....')
  res.keys.each do |i|
    temp = res[i]        
    cookie = Mechanize::Cookie.new(i, temp.value)
    cookie.domain = temp.domain
    cookie.path = temp.path
    agent.cookie_jar.add(uri,cookie)        
  end

  filedata = agent.get(url).content
  aFile = File.new("#{dir}/file.wav", 'wb')
  aFile.syswrite(filedata)
end

该文件是否可以嵌入到 html 中或缓存?

是的,它可以 !是否可以在视频和音频标签中使用数据 URI?

<audio controls="controls" autobuffer="autobuffer" autoplay="autoplay">
  <source src="data:audio/wav;base64,UklGRhwMAABXQVZFZm10IBAAAAABAAEAgD4AAIA+AAABAAgAZGF0Ya4LAACAgICAgICAgICAgICAgICAgICAgICAgICAf3hxeH+AfXZ1eHx6dnR5fYGFgoOKi42aloubq6GOjI2Op7ythXJ0eYF5aV1AOFFib32HmZSHhpCalIiYi4SRkZaLfnhxaWptb21qaWBea2BRYmZTVmFgWFNXVVVhaGdbYGhZbXh1gXZ1goeIlot1k6yxtKaOkaWhq7KonKCZoaCjoKWuqqmurK6ztrO7tbTAvru/vb68vbW6vLGqsLOfm5yal5KKhoyBeHt2dXBnbmljVlJWUEBBPDw9Mi4zKRwhIBY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CgYCBgIGAgYCBgICAgICAgICAf4B/f4B/gH+Af35/fn9+f35/fn1+fn19fn1+fX59fn19fX19fH18fXx9fH18fXx9fH18fXx8fHt8e3x7fHt8e3x7fHt8e3x7fHt8e3x7fHt8e3x7fHt8e3x8e3x7fHt8e3x7fHx8fXx9fH18fX5+fX59fn9+f35+f35/gH+Af4B/gICAgICAgICAgICAgYCBgIGAgIGAgYGBgoGCgYKBgoGCgYKBgoGCgoKDgoOCg4KDgoOCg4KDgoOCg4KDgoOCg4KDgoOCg4KDgoOCg4KDgoOCg4KDgoOCg4KDgoOCg4KDgoOCg4KCgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGBgYCBgIGAgYCBgIGAgYCBgIGAgYCBgIGAgYCBgIGAgYCAgICBgIGAgYCBgIGAgYCBgIGAgYCBgExJU1RCAAAASU5GT0lDUkQMAAAAMjAwOC0wOS0yMQAASUVORwMAAAAgAAABSVNGVBYAAABTb255IFNvdW5kIEZvcmdlIDguMAAA" />
</audio>

有没有办法获取并保存在本地?

是的你可以 !你只需要找到缓存目录:)

http://www.digitalmediaminute.com/article/626/viewing-browser-cache-in-firefox

然后正确的一点代码去获取它这个代码与 opt.1 而不是 opt.2 一起使用

def getlatestdir(newdirs)
times = Array.new
newdirs.each_with_index do |newdir,index|
  times[index] = File::mtime(newdir)
end
temp = times[0]
count = 0
times.each_with_index do |time,index|
  if temp < time
    temp = time
    count = index
  end
end
return newdirs[count] 
end

def getCacheDir
#how to get the path
#in irb enter
#require 'capybara'
#session = Capybara::Session.new(:selenium)
#session.visit('https://www.google.co.za')
#--- then open a new tab and enter about:cache
#copy the disk cache device cache directory ( from /var/... to .../T/ )
path = '/var/folders/9x/51cvmc215xx6zy9vd_64sxwc0000gn/T/'
dirs = Dir.glob(path +'*/')
newdirs = Array.new
dirs.each_with_index do |dir,index|
  if(dir.include? 'webdriver-profile')
    newdirs[newdirs.length] = dir
  end
end
the_cache_dir = getlatestdir(newdirs) + 'Cache'
return the_cache_dir
end

def saveFile
rifffile = ''
count = 0
the_cache_dir = getCacheDir
files = Dir.glob(the_cache_dir + '/*/*/*')
files.each_with_index do |file,index|
  bytes = open(file, 'rb'){|io|io.read}
  str = bytes[0].to_s + bytes[1].to_s + bytes[2].to_s + bytes[3].to_s
  if(str == 'RIFF')
    count = index
    rifffile = file
    break
  end
end
puts rifffile
filename = 'test123.wav'
#read file bytes
bytes = File.open(rifffile, 'rb'){|io|io.read}
#write file to the directory
f = File.new(filename, 'wb')
f.syswrite(bytes)
return filename
end

授予上述代码不是最大或最快的,但它完成了工作

其他选项包括从声音设备录制,或使用麦克风录制播放的声音

那将花费太长时间和太多的努力:P

总而言之,opt.1 还可以,但不是很好,opt.2 好得多 :)

ajt

于 2013-03-04T18:31:06.310 回答