js #1116
Replies: 8 comments 52 replies
-
I read some info, or another way |
Beta Was this translation helpful? Give feedback.
-
You have to port this script from JS to Python: https://github.com/gildas-lormeau/single-file-cli/blob/master/back-ends/webdriver-chromium.js. |
Beta Was this translation helpful? Give feedback.
-
How can i do that ? |
Beta Was this translation helpful? Give feedback.
-
can u give me example plz what i have to execute |
Beta Was this translation helpful? Give feedback.
-
What is unclear to you in the code I suggested? It should be quite easy to port it in Python. Unfortunately, I don't use this programming language. I'm moving this issue in the "Discussions" tab because it's not describing a bug or a feature request. |
Beta Was this translation helpful? Give feedback.
-
options = '''{
removeHiddenElements: true,
removeUnusedStyles: true,
removeUnusedFonts: true,
removeImports: true,
blockScripts: true,
blockAudios: true,
blockVideos: true,
compressHTML: true,
removeAlternativeFonts: true,
removeAlternativeMedias: true,
removeAlternativeImages: true,
groupDuplicateImages: true
}'''
def get_scripts(options):
# Write the JavaScript code to a file
with open("temp.js", "w") as f:
f.write("""
const getScripts = async options => {
return await require("./common/scripts.js").get(options);
};
getScripts(%s)
.then(result => {
console.log(JSON.stringify(result));
})
.catch(error => {
console.error(error);
});
""" % json.dumps(options))
# Run the file using Node.js
result = subprocess.run(["node", "temp.js"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout = result.stdout.decode("utf-8").strip()
stderr = result.stderr.decode("utf-8").strip()
print(stdout)
print('------')
print(stderr)
# Check if there was an error
if stderr:
raise Exception(stderr)
# Return the result
return json.loads(stdout)
def get_page_data(driver, options):
script = """
const [options, callback] = arguments;
getPageData()
.then(pageData => callback({ pageData }))
.catch(error => callback({ error: error && error.toString() }));
async function getPageData() {
const pageData = await singlefile.getPageData(options);
if (options.includeInfobar) {
await infobar.includeScript(pageData);
}
return pageData;
}
"""
result = driver.execute_async_script(script, options)
if "error" in result:
raise Exception(result["error"])
else:
return result["pageData"]
driver.execute_script(get_scripts(options))
input(2222)
print(get_page_data(driver, options))
input(3333) and got error
|
Beta Was this translation helpful? Give feedback.
-
that i have in stdout |
Beta Was this translation helpful? Give feedback.
-
i think content its my html file ? |
Beta Was this translation helpful? Give feedback.
-
I already added extension, but how can it work
I need to execute ur extension like
self.driver.execute_script(run.SingleFile)
Beta Was this translation helpful? Give feedback.
All reactions