4.2.1 by quoid · Pull Request #289 · quoid/userscripts · GitHub
Skip to content
Merged

4.2.1 #289

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
49 changes: 33 additions & 16 deletions etc/app_ios/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 30 additions & 13 deletions extension/Userscripts Extension/Functions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,34 @@ func openSaveLocation() -> Bool {
}

func validateUrl(_ urlString: String) -> Bool {
var urlChecked = urlString
// if the url is already encoded, decode it
if isEncoded(urlChecked) {
if let decodedUrl = urlChecked.removingPercentEncoding {
urlChecked = decodedUrl
} else {
err("validateUrl failed at (1), couldn't decode url, \(urlString)")
return false
}
}
// encode all urls strings
if let encodedUrl = urlChecked.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
urlChecked = encodedUrl
} else {
err("validateUrl failed at (2), couldn't percent encode url, \(urlString)")
return false
}
guard
let parts = getUrlProps(urlChecked),
let ptcl = parts["protocol"],
let path = parts["pathname"]
else {
err("validateUrl failed at (3) for \(urlString)")
return false
}
if
(!urlString.hasPrefix("https://") && !urlString.hasPrefix("http://"))
|| (!urlString.hasSuffix(".css") && !urlString.hasSuffix(".js"))
(ptcl != "https:" && ptcl != "http:")
|| (!path.hasSuffix(".css") && !path.hasSuffix(".js"))
{
return false
}
Expand Down Expand Up @@ -520,8 +545,6 @@ func updateManifestDeclarativeNetRequests(_ optionalFilesArray: [[String: Any]]
if fn == filename, let index = manifest.match[pattern]?.firstIndex(of: filename) {
manifest.match[pattern]?.remove(at: index)
update = true
} else {
err("updateManifestDeclarativeNetRequests failed at (2), \(filename)")
}
}
}
Expand All @@ -530,8 +553,6 @@ func updateManifestDeclarativeNetRequests(_ optionalFilesArray: [[String: Any]]
if fn == filename, let index = manifest.excludeMatch[pattern]?.firstIndex(of: filename) {
manifest.excludeMatch[pattern]?.remove(at: index)
update = true
} else {
err("updateManifestDeclarativeNetRequests failed at (3), \(filename)")
}
}
}
Expand All @@ -540,8 +561,6 @@ func updateManifestDeclarativeNetRequests(_ optionalFilesArray: [[String: Any]]
if fn == filename, let index = manifest.include[pattern]?.firstIndex(of: filename) {
manifest.include[pattern]?.remove(at: index)
update = true
} else {
err("updateManifestDeclarativeNetRequests failed at (4), \(filename)")
}
}
}
Expand All @@ -550,13 +569,11 @@ func updateManifestDeclarativeNetRequests(_ optionalFilesArray: [[String: Any]]
if fn == filename, let index = manifest.exclude[pattern]?.firstIndex(of: filename) {
manifest.exclude[pattern]?.remove(at: index)
update = true
} else {
err("updateManifestDeclarativeNetRequests failed at (5), \(filename)")
}
}
}
if update, !updateManifest(with: manifest) {
err("updateManifestDeclarativeNetRequests failed at (6)")
err("updateManifestDeclarativeNetRequests failed at (2)")
return false
}
}
Expand Down Expand Up @@ -1255,12 +1272,12 @@ func getCode(_ filenames: [String], _ isTop: Bool)-> [String: Any]? {
weight = normalizeWeight(weight)

// get inject-into and set default if missing
var injectInto = metadata["inject-into"]?[0] ?? "auto"
var injectInto = metadata["inject-into"]?[0] ?? "content"
let injectVals: Set<String> = ["auto", "content", "page"]
let runAtVals: Set<String> = ["context-menu", "document-start", "document-end", "document-idle"]
// if either is invalid use default value
if !injectVals.contains(injectInto) {
injectInto = "auto"
injectInto = "content"
}
if !runAtVals.contains(runAt) {
runAt = "document-end"
Expand Down
27 changes: 26 additions & 1 deletion extension/Userscripts Extension/Resources/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ browser.runtime.sendMessage({name: "REQ_USERSCRIPTS", uid: uid}, response => {
data = response;
for (let i = 0; i < data.files.js.length; i++) {
const userscript = data.files.js[i];
if (
userscript.scriptObject?.grants?.length
&& (
userscript.scriptObject["inject-into"] === "auto"
|| userscript.scriptObject["inject-into"] === "page"
)
) {
userscript.scriptObject["inject-into"] = "content";
console.warn(`${userscript.scriptObject.filename} had it's @inject-value automatically set to "content" because it has @grant values - see: https://github.com/quoid/userscripts/issues/252#issuecomment-1136637700`);
}
processJS(
userscript.scriptObject.name,
userscript.scriptObject.filename,
Expand Down Expand Up @@ -90,7 +100,22 @@ function injectJS(name, filename, code, scope, fallback) {

function injectCSS(name, code) {
console.info(`Injecting ${name} %c(css)`, "color: #60f36c");
browser.runtime.sendMessage({name: "API_ADD_STYLE_SYNC", css: code});
// Safari lacks full support for tabs.insertCSS
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs/insertCSS
// specifically frameId and cssOrigin
// if support for those details keys arrives, the method below can be used
// NOTE: manifest V3 does support frameId, but not origin
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/scripting/insertCSS

// browser.runtime.sendMessage({name: "API_ADD_STYLE_SYNC", css: code});

// write the css code to head of the document
let wrapper = "const tag = document.createElement(\"style\");\n";
wrapper += `tag.textContent = \`${code}\`;`;
wrapper += "\ndocument.head.appendChild(tag);";
// eval the code directly into the context of the content script (not page context)
// wrapper += "console.log(window.browser)"; // this validates the execution env
eval(wrapper);
}

function cspFallback(e) {
Expand Down
2 changes: 1 addition & 1 deletion extension/Userscripts Extension/Resources/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"default_locale": "en",
"name": "__MSG_extension_name__",
"description": "__MSG_extension_description__",
"version": "4.2.0",
"version": "4.2.1",
"icons": {
"48": "images/icon-48.png",
"96": "images/icon-96.png",
Expand Down
32 changes: 16 additions & 16 deletions extension/Userscripts.xcodeproj/project.pbxproj
Loading