2024-02-07 16:06:33 +05:00
|
|
|
import os
|
|
|
|
|
|
|
|
def generate_metadata_file(input_file, output_file):
|
|
|
|
with open(input_file, 'r', encoding='utf-8') as f:
|
|
|
|
lines = f.readlines()
|
|
|
|
|
|
|
|
start_index = None
|
|
|
|
end_index = None
|
|
|
|
|
|
|
|
for i, line in enumerate(lines):
|
|
|
|
if line.startswith("// ==UserScript=="):
|
|
|
|
start_index = i
|
|
|
|
elif line.startswith("// ==/UserScript=="):
|
|
|
|
end_index = i
|
|
|
|
break
|
|
|
|
|
|
|
|
if start_index is not None and end_index is not None:
|
|
|
|
metadata_lines = lines[start_index:end_index + 1]
|
|
|
|
with open(output_file, 'w', encoding='utf-8') as f:
|
|
|
|
f.writelines(metadata_lines)
|
|
|
|
print("Metadata extracted successfully!")
|
|
|
|
else:
|
|
|
|
print("Could not find metadata in the input file.")
|
|
|
|
|
|
|
|
|
|
|
|
# Function to read and modify .js files
|
|
|
|
def process_js_files(folder_path, target_file):
|
2024-02-25 18:10:27 +05:00
|
|
|
|
|
|
|
# Read the content of the target file
|
|
|
|
with open(target_file, 'r', encoding='utf-8') as f:
|
|
|
|
target_file_lines = f.readlines()
|
|
|
|
|
2024-02-07 16:06:33 +05:00
|
|
|
# Initialize lists to store lines to be added
|
2024-02-25 18:10:27 +05:00
|
|
|
grant_lines = []
|
2024-02-07 16:06:33 +05:00
|
|
|
match_lines = []
|
2024-02-25 18:10:27 +05:00
|
|
|
include_lines = []
|
2024-02-07 16:06:33 +05:00
|
|
|
require_lines = []
|
2024-03-22 18:03:24 +05:00
|
|
|
resource_lines = []
|
2024-02-07 16:06:33 +05:00
|
|
|
after_user_script_lines = []
|
|
|
|
|
|
|
|
# Traverse through the files in the folder
|
2024-02-15 18:11:51 +05:00
|
|
|
for filename in sorted(os.listdir(folder_path)):
|
2024-02-07 16:06:33 +05:00
|
|
|
if filename.endswith(".js"):
|
|
|
|
with open(os.path.join(folder_path, filename), 'r', encoding='utf-8') as f:
|
|
|
|
lines = f.readlines()
|
|
|
|
|
2024-02-25 18:10:27 +05:00
|
|
|
# Find lines with "@match" or "@include" or "@require" or "@grant". Add them if they are not already in the target file.
|
2024-02-07 16:06:33 +05:00
|
|
|
for line in lines:
|
2024-03-22 18:03:24 +05:00
|
|
|
if "@match" in line or "@include" in line or "@resource" in line or "@require" in line or "@grant" in line:
|
2024-02-25 18:10:27 +05:00
|
|
|
if line not in target_file_lines:
|
|
|
|
if line.startswith("// @grant"):
|
|
|
|
grant_lines.append(line)
|
|
|
|
elif line.startswith("// @match"):
|
|
|
|
match_lines.append(line)
|
|
|
|
elif line.startswith("// @include"):
|
|
|
|
include_lines.append(line)
|
|
|
|
elif line.startswith("// @require"):
|
|
|
|
require_lines.append(line)
|
2024-03-22 18:03:24 +05:00
|
|
|
elif line.startswith("// @resource"):
|
|
|
|
require_lines.append(line)
|
2024-02-07 16:06:33 +05:00
|
|
|
|
|
|
|
# Find lines after "// ==/UserScript=="
|
|
|
|
after_user_script = False
|
|
|
|
for line in lines:
|
|
|
|
if after_user_script:
|
|
|
|
after_user_script_lines.append(line)
|
|
|
|
elif "// ==/UserScript==" in line:
|
|
|
|
after_user_script = True
|
|
|
|
|
|
|
|
# Write gathered information to target_file
|
|
|
|
with open(target_file, 'r+', encoding='utf-8') as f:
|
|
|
|
content = f.readlines()
|
|
|
|
exclude_index = next((i for i, line in enumerate(content) if "@exclude" in line), None)
|
|
|
|
if exclude_index is not None:
|
|
|
|
# Add match_lines and require_lines before the first @exclude line
|
2024-03-22 18:03:24 +05:00
|
|
|
content = content[:exclude_index] + grant_lines + match_lines + include_lines + require_lines + resource_lines + content[exclude_index:]
|
2024-02-07 16:06:33 +05:00
|
|
|
# Add lines after "// ==/UserScript=="
|
|
|
|
content.extend(after_user_script_lines)
|
|
|
|
f.seek(0)
|
|
|
|
f.writelines(content)
|
|
|
|
|
2024-02-14 06:31:19 +05:00
|
|
|
|
2024-02-25 18:10:27 +05:00
|
|
|
# Clean match and include lines and add them to supported_sites.txt
|
2024-02-14 06:31:19 +05:00
|
|
|
cleaned_lines = []
|
|
|
|
for line in match_lines:
|
2024-02-25 18:10:27 +05:00
|
|
|
cleaned_line = line.strip().replace("// @match", "").strip()
|
|
|
|
cleaned_lines.append(cleaned_line)
|
|
|
|
for line in include_lines:
|
|
|
|
cleaned_line = line.strip().replace("// @include", "").strip()
|
2024-02-14 06:31:19 +05:00
|
|
|
cleaned_lines.append(cleaned_line)
|
|
|
|
with open("supported_sites.txt", 'a', encoding='utf-8') as f:
|
|
|
|
for line in cleaned_lines:
|
|
|
|
f.write(line + '\n')
|
|
|
|
|
2024-02-07 16:06:33 +05:00
|
|
|
# Main function to execute the process
|
|
|
|
def main():
|
2024-02-07 20:18:23 +05:00
|
|
|
fixes_folder = "./extra_bypasses"
|
2024-02-07 16:06:33 +05:00
|
|
|
target_file = "Bypass_All_Shortlinks.user.js"
|
|
|
|
process_js_files(fixes_folder, target_file)
|
|
|
|
print("Modification complete.")
|
|
|
|
generate_metadata_file("Bypass_All_Shortlinks.user.js", "Bypass_All_Shortlinks.meta.js")
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|