Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
u-Sir authored Dec 26, 2024
1 parent e59c2ea commit 114a601
Show file tree
Hide file tree
Showing 8 changed files with 386 additions and 136 deletions.
130 changes: 71 additions & 59 deletions chromium/background.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,75 @@
chrome.runtime.onInstalled.addListener(() => {
chrome.declarativeNetRequest.updateDynamicRules({
addRules: [
// Rule to modify headers (remove CSP, X-Frame-Options, and Cookies) for all domains
{
id: 1,
priority: 1,
action: {
type: "modifyHeaders",
responseHeaders: [
{ header: "Content-Security-Policy", operation: "remove" },
{ header: "X-Frame-Options", operation: "remove" },
{ header: "Cookie", operation: "remove" }
]
},
condition: {
urlFilter: "*://*/*", // Applies to all URLs
resourceTypes: ["sub_frame"]
chrome.declarativeNetRequest.getDynamicRules((rules) => {
const ruleIds = rules.map((rule) => rule.id);

if (ruleIds.length > 0) {
chrome.declarativeNetRequest.updateDynamicRules(
{ removeRuleIds: ruleIds },
() => {

chrome.declarativeNetRequest.updateDynamicRules({
addRules: [
// Rule to modify headers (remove CSP, X-Frame-Options, and Cookies) for all domains
{
id: 1,
priority: 1,
action: {
type: "modifyHeaders",
responseHeaders: [
{ header: "Content-Security-Policy", operation: "remove" },
{ header: "X-Frame-Options", operation: "remove" },
{ header: "Cookie", operation: "remove" }
]
},
condition: {
urlFilter: "*://*/*", // Applies to all URLs
resourceTypes: ["sub_frame"]
}
},
// Rule to set the mobile User-Agent for all domains
{
id: 2,
priority: 2,
action: {
type: "modifyHeaders",
requestHeaders: [
{
header: "User-Agent",
operation: "set",
value:
"Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1"
}
]
},
condition: {
urlFilter: "*://*/*", // Applies to all URLs
resourceTypes: ["sub_frame"]
}
},
// Rule to set desktop User-Agent for xiaohongshu.com
{
id: 3,
priority: 3, // Lower priority than rule 2
action: {
type: "modifyHeaders",
requestHeaders: [
{
header: "User-Agent",
operation: "set",
value: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
}
]
},
condition: {
urlFilter: "https://www.xiaohongshu.com/*", // Only applies to xiaohongshu.com domain
resourceTypes: ["sub_frame"]
}
}
],
removeRuleIds: [1, 2, 3] // Clean up previous rules if necessary
});
}
},
// Rule to set the mobile User-Agent for all domains
{
id: 2,
priority: 2,
action: {
type: "modifyHeaders",
requestHeaders: [
{
header: "User-Agent",
operation: "set",
value:
"Mozilla/5.0 (iPhone; CPU iPhone OS 16_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1"
}
]
},
condition: {
urlFilter: "*://*/*", // Applies to all URLs
resourceTypes: ["sub_frame"]
}
},
// Rule to set desktop User-Agent for xiaohongshu.com
{
id: 3,
priority: 3, // Lower priority than rule 2
action: {
type: "modifyHeaders",
requestHeaders: [
{
header: "User-Agent",
operation: "set",
value: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"
}
]
},
condition: {
urlFilter: "https://www.xiaohongshu.com/*", // Only applies to xiaohongshu.com domain
resourceTypes: ["sub_frame"]
}
}
],
removeRuleIds: [1, 2, 3] // Clean up previous rules if necessary
);
}
});
});
21 changes: 21 additions & 0 deletions chromium/custom_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,24 @@
</g>

</svg></button>
<button id="pcUAButton"><svg width="20px" height="20px" viewBox="0 0 20 20" version="1.1"
xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" fill="#000000">
<g id="SVGRepo_bgCarrier" stroke-width="0"></g>
<g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g>
<g id="SVGRepo_iconCarrier">
<desc>Created with Sketch.</desc>
<defs> </defs>
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g id="Dribbble-Light-Preview" transform="translate(-100.000000, -7159.000000)" fill="#cecece">
<g id="icons" transform="translate(56.000000, 160.000000)">
<path
d="M62.0006998,7012 C62.0006998,7012.552 61.5528565,7013 61.0010496,7013 L47.0059479,7013 C46.4541411,7013 46.0062978,7012.552 46.0062978,7012 L46.0062978,7002 C46.0062978,7001.448 46.4541411,7001 47.0059479,7001 L61.0010496,7001 C61.5528565,7001 62.0006998,7001.448 62.0006998,7002 L62.0006998,7012 Z M61.9907033,6999 L45.9963013,6999 L45.9923027,6999 C44.888689,6999 44,6999.915 44,7001.02 L44.0069976,7001.04 L44.0069976,7013.04 L44,7013.02 C44,7014.125 44.888689,7015 45.9923027,7015 L45.9963013,7015 L51.9902034,7015 C52.5430099,7015 53.0038487,7015.468 53.0038487,7016.02 L53.0038487,7016.01 C53.0038487,7016.562 52.5430099,7017 51.9902034,7017 L48.9912531,7017 C48.4394462,7017 48.005598,7017.468 48.005598,7018.02 L48.005598,7018.01 C48.005598,7018.562 48.4394462,7019 48.9912531,7019 L58.9877543,7019 C59.5405608,7019 60.0013995,7018.572 60.0013995,7018.02 L60.0013995,7018.01 C60.0013995,7017.458 59.5405608,7017 58.9877543,7017 L55.9888039,7017 C55.4369971,7017 55.0031489,7016.572 55.0031489,7016.02 L55.0031489,7016.01 C55.0031489,7015.458 55.4369971,7015 55.9888039,7015 L61.9907033,7015 L61.9977008,7015.02 C63.0993152,7015.02 64,7014.146 64,7013.043 L64,7013.04 L64,7001.04 L64,7001.037 C64,6999.934 63.0993152,6999.02 61.9977008,6999.02 L61.9907033,6999 Z"
id="desktop-[#232]"> </path>
</g>
</g>
</g>
</g>
</svg></button>
</div>
<div id="settingsContainer" style="display: none;">

Expand All @@ -234,6 +252,9 @@
<label>
↕ : <input id="iframeHeight" type="number" placeholder="667" />
</label>
<label>
→| |←: <input id="iframeGap" type="number" placeholder="20" />
</label>
<button id="saveSettingsButton"></button>
</div>
<div id="iframeContainer"></div>
Expand Down
106 changes: 99 additions & 7 deletions chromium/custom_page.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,33 @@ const iframeContainer = document.getElementById("iframeContainer");
const addButton = document.getElementById("addButton");
const settingsButton = document.getElementById("settingsButton");
const editAllButton = document.getElementById("editAllButton");
const pcUAButton = document.getElementById("pcUAButton");
const settingsContainer = document.getElementById("settingsContainer");
const iframeWidthInput = document.getElementById("iframeWidth");
const iframeHeightInput = document.getElementById("iframeHeight");
const iframeGapInput = document.getElementById("iframeGap");
const saveSettingsButton = document.getElementById("saveSettingsButton");

let iframeWidth = 375; // Default width
let iframeHeight = 667; // Default height
let iframeGap = 10; // Default gap

// Load settings and URLs when the page opens
document.addEventListener("DOMContentLoaded", () => {
chrome.storage.local.get(["iframeUrls", "iframeSettings"], (data) => {
const urls = data.iframeUrls || [];
const settings = data.iframeSettings || { width: 375, height: 667 };
const settings = data.iframeSettings || { width: 375, height: 667, gap: 10 };

iframeWidth = settings.width;
iframeHeight = settings.height;
iframeGap = settings.gap;

iframeWidthInput.value = iframeWidth;
iframeHeightInput.value = iframeHeight;
iframeGapInput.value = iframeGap;

const iframeContainer = document.querySelector('#iframeContainer');
iframeContainer.style.gap = `${iframeGap}px`; // Set the new gap

urls.forEach((url) => createIframe(url));
});
Expand Down Expand Up @@ -316,15 +325,18 @@ settingsButton.addEventListener("click", () => {
saveSettingsButton.addEventListener("click", () => {
const newWidth = parseInt(iframeWidthInput.value, 10);
const newHeight = parseInt(iframeHeightInput.value, 10);
const newGap = parseInt(iframeGapInput.value, 10);

if (newWidth > 0 && newHeight > 0) {
if (newWidth > 0 && newHeight > 0 && newGap > 0) {
iframeWidth = newWidth;
iframeHeight = newHeight;
iframeGap = newGap;

chrome.storage.local.set({
iframeSettings: {
width: iframeWidth,
height: iframeHeight,
gap: iframeGap
},
}, () => {
window.location.reload();
Expand All @@ -341,24 +353,104 @@ editAllButton.addEventListener("click", async () => {
chrome.storage.local.get('iframeUrls', (data) => {
// If URLs exist, format them as a semicolon-separated list
const currentUrls = data.iframeUrls || [];
const formattedUrls = currentUrls.join(";"); // Join URLs with semicolon and space
const formattedUrls = currentUrls.join(";"); // Join URLs with semicolon

// Create a prompt to allow the user to edit the URLs, and show current URLs as a default value
const newUrls = prompt("URLs: example.com;https://www.example2.com", formattedUrls);

// If the user entered some new URLs, process them
if (newUrls !== null) {
// Split by semicolon and remove any leading/trailing spaces from each URL
const updatedUrls = newUrls.split(";").map(url => url.trim());
// Split by semicolon, trim spaces, and validate URLs
const updatedUrls = newUrls.split(";")
.map(url => url.trim()) // Trim each URL
.filter(url => url !== "") // Remove empty strings
.map(url => {
// Prepend https:// if the URL doesn't start with http:// or https://
if (!/^https?:\/\//i.test(url)) {
url = `https://${url}`;
}
return url;
});

// Save the updated URLs back to chrome.storage.local
chrome.storage.local.set({ 'iframeUrls': updatedUrls }, () => {

// alert("URLs updated successfully!");
// Reload the page to apply the changes immediately
window.location.reload();
});
}
});
});



pcUAButton.addEventListener("click", () => {
chrome.declarativeNetRequest.getDynamicRules((rules) => {
const pcUAValue =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36";
let maxRuleId = rules.length > 0 ? Math.max(...rules.map((rule) => rule.id)) : 0;

// Filter existing rules that modify User-Agent to PC UA
const uaRules = rules.filter((rule) =>
rule.action?.requestHeaders?.some(
(header) => header.header === "User-Agent" && header.value === pcUAValue
)
);

// Extract existing URL filters
const urlFilters = uaRules.map((rule) => rule.condition.urlFilter).join(";");

// Show prompt with current URLs
const input = prompt(
"PC User-Agent URLs: \n*.example.com;https://www.example2.com/path/*",
urlFilters || ""
);

if (input !== null) {
const newUrls = input
.split(";")
.map((url) => url.trim())
.filter(Boolean); // Remove empty or invalid entries

const existingFilters = new Set(uaRules.map((rule) => rule.condition.urlFilter));

// Determine rules to add and remove
const urlsToRemove = Array.from(existingFilters).filter((url) => !newUrls.includes(url));
const urlsToAdd = newUrls.filter((url) => !existingFilters.has(url));

const rulesToRemove = uaRules
.filter((rule) => urlsToRemove.includes(rule.condition.urlFilter))
.map((rule) => rule.id);

const newRules = urlsToAdd.map((url) => ({
id: ++maxRuleId,
priority: 3,
action: {
type: "modifyHeaders",
requestHeaders: [
{
header: "User-Agent",
operation: "set",
value: pcUAValue,
},
],
},
condition: {
urlFilter: url,
resourceTypes: ["sub_frame"],
},
}));

// Update dynamic rules
chrome.declarativeNetRequest.updateDynamicRules(
{
addRules: newRules,
removeRuleIds: rulesToRemove,
},
() => {
console.log("Rules updated:", { added: newRules, removed: rulesToRemove });
window.location.reload();
}
);
}
});
});
4 changes: 2 additions & 2 deletions chromium/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "Hall of iFrame · 页问",
"version": "1.4",
"name": "Tabless",
"version": "1.5",
"permissions": [
"declarativeNetRequest",
"storage"
Expand Down
Loading

0 comments on commit 114a601

Please sign in to comment.