Skip to content

Commit

Permalink
browser.scripting.executeScript: Inject script in 'MAIN' world when s…
Browse files Browse the repository at this point in the history
…pecified.

https://bugs.webkit.org/show_bug.cgi?id=268560
rdar://122113121

Reviewed by Brian Weinstein.

We should be using the pageWorld when 'MAIN' is specified for scripting.executeScript().
We do this already for RegisteredContentScripts, so no need for a fix there.

* Source/WebKit/UIProcess/Extensions/Cocoa/API/WebExtensionContextAPIScriptingCocoa.mm:
(WebKit::WebExtensionContext::scriptingExecuteScript):
* Tools/TestWebKitAPI/Tests/WebKitCocoa/WKWebExtensionAPIScripting.mm:
(TestWebKitAPI::TEST):

Canonical link: https://commits.webkit.org/273937@main
  • Loading branch information
kiaraarose committed Feb 1, 2024
1 parent 5a5af81 commit 49b7512
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@
}

auto scriptPairs = getSourcePairsForParameters(parameters, m_extension);
executeScript(scriptPairs, webView, *m_contentScriptWorld, tab.get(), parameters, *this, [completionHandler = WTFMove(completionHandler)](InjectionResultHolder& injectionResults) mutable {
Ref executionWorld = parameters.world == WebExtensionContentWorldType::Main ? API::ContentWorld::pageContentWorld() : *m_contentScriptWorld;

executeScript(scriptPairs, webView, executionWorld, tab.get(), parameters, *this, [completionHandler = WTFMove(completionHandler)](InjectionResultHolder& injectionResults) mutable {
completionHandler(injectionResults.results, std::nullopt);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,47 @@
[manager loadAndRun];
}

TEST(WKWebExtensionAPIScripting, World)
{
TestWebKitAPI::HTTPServer server({
{ "/"_s, { { { "Content-Type"_s, "text/html"_s } }, "<script> const world = 'MAIN'; </script>"_s } }
}, TestWebKitAPI::HTTPServer::Protocol::Http);

auto *backgroundScript = Util::constructScript(@[
@"const tabs = await browser?.tabs?.query({ active: true, currentWindow: true })",
@"const tabId = tabs[0].id",

@"function testWorld() {",
@" if (world)",
@" browser.test.notifyPass()",
@" else",
@" console.log(undefined)",
@"}",

@"var results = await browser?.scripting?.executeScript( { target: { tabId: tabId, world: 'ISOLATED'}, func: testWorld })",
@"browser.test.assertEq(results[0]?.error, 'A JavaScript exception occurred')",

@"await browser?.scripting?.executeScript( { target: { tabId: tabId, world: 'MAIN'}, func: testWorld })",
]);


static auto *resources = @{
@"background.js": backgroundScript,
};

auto extension = adoptNS([[_WKWebExtension alloc] _initWithManifestDictionary:scriptingManifest resources:resources]);
auto manager = adoptNS([[TestWebExtensionManager alloc] initForExtension:extension.get()]);

auto *urlRequest = server.requestWithLocalhost();
auto *url = urlRequest.URL;

auto *matchPattern = [_WKWebExtensionMatchPattern matchPatternWithScheme:url.scheme host:url.host path:@"/*"];
[manager.get().context setPermissionStatus:_WKWebExtensionContextPermissionStatusGrantedExplicitly forMatchPattern:matchPattern];
[manager.get().defaultTab.mainWebView loadRequest:urlRequest];

[manager loadAndRun];
}

TEST(WKWebExtensionAPIScripting, RegisterContentScripts)
{
TestWebKitAPI::HTTPServer server({
Expand Down

0 comments on commit 49b7512

Please sign in to comment.