Skip to content

Commit

Permalink
Use chrome.scripting.executeScript to open bookmarklets in the Vomnibar
Browse files Browse the repository at this point in the history
This fixes #4329.

Note that bookmarklets will still raise an error if the site has a restrictive CSP. For the full
solution, we need a new API from Chrome. This is tracked in #4331.
  • Loading branch information
philc committed Oct 11, 2023
1 parent dff67c3 commit 10524a0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
32 changes: 30 additions & 2 deletions background_scripts/tab_operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,39 @@
// TODO(philc): Convert these to Promise-based APIs.

// Opens the url in the current tab.
// If the URL is a JavaScript snippet, execute that snippet in the current tab.
function openUrlInCurrentTab(request) {
// Note that when injecting JavaScript, it's subject to the site's CSP. Sites with strict CSPs
// (like github.com, developer.mozilla.org) will raise an error when we try to run this code. See
// https://github.com/philc/vimium/issues/4331.
if (Utils.hasJavascriptPrefix(request.url)) {
const tabId = request.tabId;
const frameId = request.frameId;
chrome.tabs.sendMessage(tabId, { frameId, handler: "executeUserScript", script: request.url });
const scriptingArgs = {
target: { tabId: request.tabId },
func: (text) => {
const prefix = "javascript:";
text = text.slice(prefix.length).trim();
text = decodeURIComponent(text);
try {
text = decodeURIComponent(text);
} catch {
// Swallow
}
const el = document.createElement("script");
el.textContent = text;
document.head.appendChild(el);
},
args: [request.url],
};

if (!BgUtils.isFirefox()) {
// The MAIN world -- where the webpage runs -- is less privileged than the ISOLATED world.
// Specifying a world is required for Chrome, but not Firefox.
// As of Firefox 118, specifying "MAIN" as the world is not yet supported.
args.world = "MAIN";
}

chrome.scripting.executeScript(scriptingArgs);
} else {
chrome.tabs.update(request.tabId, { url: Utils.convertToUrl(request.url) });
}
Expand Down
3 changes: 0 additions & 3 deletions content_scripts/vimium_frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,6 @@ const initializePreDomReady = async function () {
showMessage(request) {
HUD.show(request.message, 2000);
},
executeUserScript(request) {
DomUtils.injectUserScript(request.script);
},
};

Utils.addChromeRuntimeOnMessageListener(
Expand Down
16 changes: 0 additions & 16 deletions lib/dom_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -637,22 +637,6 @@ const DomUtils = {
style.textContent = Settings.get("userDefinedLinkHintCss");
document.head.appendChild(style);
},

// Inject user Javascript.
injectUserScript(text) {
if (text.slice(0, 11) === "javascript:") {
text = text.slice(11).trim();
try {
text = decodeURIComponent(text);
} catch {
// Swallow
}
}
const script = document.createElement("script");
script.textContent = text;
// TODO(philc): Can we remove this return?
return document.head.appendChild(script);
},
};

window.DomUtils = DomUtils;

0 comments on commit 10524a0

Please sign in to comment.