Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update ClickToLoad to use shared C-S-S #2162

Merged
merged 62 commits into from
Jun 14, 2024
Merged
Changes from 1 commit
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
01c8a53
swap embedded user script with CSS one
ladamski Feb 6, 2024
cf561f1
fire "displayCLickToLoadPlaceholders" on load completion
ladamski Feb 7, 2024
98cb9c5
disable old CTL user script
ladamski Feb 12, 2024
8a3f6d6
test - trigger CTL on FB tracker match
ladamski Feb 12, 2024
367f031
switch to isolated scope, hook up overlay trigger
ladamski Feb 13, 2024
e19f103
use v6 TDS, updated config
ladamski Mar 2, 2024
ae481dc
move CTL code to subfolder
ladamski Mar 2, 2024
50f1411
update CTL rule management to use main TDS
ladamski Mar 2, 2024
b603ed7
WIP rule splitting for custom actions
ladamski Mar 5, 2024
f971774
cleanup FB protection state management
ladamski Mar 7, 2024
daf57ad
update embedded config and TDS
ladamski Mar 16, 2024
0d2c7a4
remove dead code
ladamski Mar 16, 2024
3a0c7fd
switch back to separate blocking CTL rule set
ladamski Mar 18, 2024
7156d78
switch back to previous CTL rules, part 2
ladamski Mar 20, 2024
fd7287e
combine rules for surrogates user script
ladamski Mar 21, 2024
7025ba1
cleanup surrogate code to remove need for vars
ladamski Apr 5, 2024
d3d0c87
update embeds
ladamski Apr 11, 2024
4879680
update config clickToPlay -> clickToLoad
ladamski Apr 11, 2024
909e53b
burn with fire (dead code)
ladamski Apr 11, 2024
be1a4ea
dead code cleanup part 2
ladamski Apr 15, 2024
12cce92
clarify method naming
ladamski Apr 15, 2024
7ed7200
handle empty ruleset in in surrogate generation (unit tests)
ladamski Apr 16, 2024
b249af6
use fallback TDS for CTL test vs old embedded CTL TDS
ladamski Apr 17, 2024
a5e1287
bump TDS test to v6
ladamski Apr 17, 2024
a93d49d
remove remapping of custom action .blockCtlFB to .block
ladamski Apr 19, 2024
5069df9
remove dead fonts, post-merge cleanup
ladamski Apr 23, 2024
f77d0c7
TEMP disable CTL assertions while testing
ladamski Apr 24, 2024
7c3f672
address @MainActor warnings, remove debug spam
ladamski Apr 24, 2024
1bc59ed
lint nit
ladamski Apr 24, 2024
841de53
try special casing NavigationType.backForward for CTL
ladamski Apr 24, 2024
838d6f8
remove MD5 func
ladamski Apr 30, 2024
abdb4f0
move FB entity string to const in FBProtectionTabExtension
ladamski Apr 30, 2024
1999c4a
update splitter logic per PR review, fix test
ladamski May 1, 2024
3ca418b
Update CTL rule splitting logic with alternate path (#2733)
ladamski May 1, 2024
95a798c
update rule combining logic per last rule splitting update
ladamski May 2, 2024
1856498
remove dead ClickToLoadModel code
ladamski May 21, 2024
4a05050
Update ClickToLoadRulesSplitter.swift
ladamski May 21, 2024
0072d09
code review nits
ladamski May 21, 2024
2a61134
Merge branch 'main' into la/update-ctl
ladamski May 21, 2024
d19a19f
cleanup FIXME
ladamski May 21, 2024
223b434
update to BSK WIP branch
ladamski May 21, 2024
cb42dd6
Update proj file and BSK ref
jaceklyp May 21, 2024
098696e
remove unused var
ladamski May 22, 2024
b01f13e
update CTL action to blockCTLFB
ladamski May 23, 2024
084d24d
add CTL test for rules splitting
ladamski May 23, 2024
2a2cf71
test nits
ladamski May 23, 2024
85cc309
move ClickToLoadRulesSpliter to BSK
ladamski May 31, 2024
3673f70
Update ClickToLoadTDSTests.swift
ladamski May 31, 2024
6ca5394
move ClickToLoadRulesSplitter to BSK
ladamski May 31, 2024
d55102c
review nits
ladamski May 31, 2024
77993dd
Update ClickToLoadTDSTests.swift
ladamski Jun 3, 2024
13af034
test tweaks
ladamski Jun 7, 2024
f5c56fd
Update ClickToLoadTDSTests.swift
ladamski Jun 8, 2024
24566fd
remove forward/back nav exception in FB protections
ladamski Jun 12, 2024
98c095b
revert config URL to prod and update embeds
ladamski Jun 13, 2024
2d04a7c
release BSK
ladamski Jun 14, 2024
0479655
update BSK dep
ladamski Jun 14, 2024
84d76eb
Merge branch 'main' into la/update-ctl
ladamski Jun 14, 2024
ef74977
Restore NetP Package.swift
bwaresiak Jun 14, 2024
388d73e
version bump to 1.93.0
ladamski Jun 14, 2024
a0ab778
Revert "version bump to 1.93.0"
ladamski Jun 14, 2024
5e5feb1
nit
ladamski Jun 14, 2024
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
Prev Previous commit
Next Next commit
move CTL code to subfolder
ladamski committed Apr 23, 2024
commit ae481dcba809f0682496ccc0671fe23eeeb4e162
797 changes: 791 additions & 6 deletions DuckDuckGo.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ import Foundation

struct ClickToLoadModel {

// DELETE ME!
ladamski marked this conversation as resolved.
Show resolved Hide resolved
private static func loadFile(name: String) -> String? {
let pathPrefix = "social_images/"
let fileArgs = name.split(separator: ".")
306 changes: 306 additions & 0 deletions DuckDuckGo/ContentBlocker/ClickToLoad/ClickToLoadUserScript.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,306 @@
//
// ClickToLoadUserScript.swift
//
// Copyright © 2021 DuckDuckGo. All rights reserved.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2024 ;)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this file dates back to 2021, it was moved when I created the CTL subfolder. Since I'm moving the splitter to BSK, I removed this new folder to simplify the diff

//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import WebKit
import Common
import UserScript

protocol ClickToLoadUserScriptDelegate: AnyObject {

func clickToLoadUserScriptAllowFB() -> Bool
}

final class ClickToLoadUserScript: NSObject, WKNavigationDelegate, Subfeature {

weak var broker: UserScriptMessageBroker?
weak var webView: WKWebView?

weak var delegate: ClickToLoadUserScriptDelegate?

#if DEBUG
var devMode: Bool = true
#else
var devMode: Bool = false
#endif

// this isn't an issue to be set to 'all' because the page
public let messageOriginPolicy: MessageOriginPolicy = .all
public let featureName: String = "clickToLoad"

// MARK: - Subfeature

public func with(broker: UserScriptMessageBroker) {
self.broker = broker
}

// MARK: - MessageNames

enum MessageNames: String, CaseIterable {
case getClickToLoadState
case unblockClickToLoadContent
case updateFacebookCTLBreakageFlags
case addDebugFlag
}

public func handler(forMethodNamed methodName: String) -> Handler? {
switch MessageNames(rawValue: methodName) {
case .getClickToLoadState:
return handleGetClickToLoadState
case .unblockClickToLoadContent:
return handleUnblockClickToLoadContent
case .updateFacebookCTLBreakageFlags:
return handleDebugFlagsMock
case .addDebugFlag:
return handleDebugFlagsMock
default:
assertionFailure("ClickToLoadUserScript: Failed to parse User Script message: \(methodName)")
return nil
}
}

private func handleGetClickToLoadState(params: Any, message: UserScriptMessage) -> Encodable? {
webView = message.messageWebView
print("handleGetClickToLoadState for url \(String(describing: message)) for webView \(webView?.url)")
return [
"devMode": devMode,
"youtubePreviewsEnabled": false
]
}

private func handleUnblockClickToLoadContent(params: Any, message: UserScriptMessage) -> Encodable? {
struct UnblockMessage: Decodable {
let action: String
let isLogin: Bool
let isSurrogateLogin: Bool
let entity: String
}
ladamski marked this conversation as resolved.
Show resolved Hide resolved

guard let delegate = delegate else { return false }

// only worry about CTL FB for now
return delegate.clickToLoadUserScriptAllowFB()
}

private func handleDebugFlagsMock(params: Any, message: UserScriptMessage) -> Encodable? {
// breakage flags not supported on Mac yet
return nil
}

// swiftlint:disable function_body_length
public func displayClickToLoadPlaceholders() {
print("displayClickToLoadPlaceholders for url \(String(describing: webView?.url)) for broker \(broker)")
if let webView = webView {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

guard let webView else { return }

let fbSurrogate = """
(() => {
'use strict';
console.warn('fb-sdk document.currentScript.src', document.currentScript, document.currentScript?.src);
console.warn('in fbSurrogate, location is', window.location.href);
debugger;
return;
window.fbTest = "inline surrogate";
const facebookEntity = 'Facebook, Inc.';
const originalFBURL = 'https://connect.facebook.net/en_US/sdk.js?XFBML=false' //FIXME: document.currentScript.src;
let siteInit = function () {};
let fbIsEnabled = false;
let initData = {};
let runInit = false;
const parseCalls = [];
const popupName = Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 12);
const fbLogin = {
callback: function () {},
params: undefined,
shouldRun: false
};
function messageAddon (detailObject) {
detailObject.entity = facebookEntity;
const event = new CustomEvent('ddg-ctp', {
detail: detailObject,
bubbles: false,
cancelable: false,
composed: false
});
dispatchEvent(event);
}
/**
* When setting up the Facebook SDK, the site may define a function called window.fbAsyncInit.
* Once the SDK loads, it searches for and calls window.fbAsyncInit. However, some sites may
* not use this, and just call FB.init directly at some point (after ensuring that the script has loaded).
*
* Our surrogate (defined below in window.FB) captures calls made to init by page scripts. If at a
* later point we load the real sdk here, we then re-call init with whatever arguments the page passed in
* originally. The runInit param should be true when a page has called init directly.
* Because we put it in asyncInit, the flow will be something like:
*
* FB SDK loads -> SDK calls window.fbAsyncInit -> Our function calls window.FB.init (maybe) ->
* our function calls original fbAsyncInit (if it existed)
*/
function enableFacebookSDK () {
if (!fbIsEnabled) {
window.FB = undefined;
window.fbAsyncInit = function () {
if (runInit && initData) {
window.FB.init(initData);
}
siteInit();
if (fbLogin.shouldRun) {
window.FB.login(fbLogin.callback, fbLogin.params);
}
};
const fbScript = document.createElement('script');
fbScript.setAttribute('crossorigin', 'anonymous');
fbScript.setAttribute('async', '');
fbScript.setAttribute('defer', '');
fbScript.src = originalFBURL;
fbScript.onload = function () {
for (const node of parseCalls) {
window.FB.XFBML.parse.apply(window.FB.XFBML, node);
}
};
document.head.appendChild(fbScript);
fbIsEnabled = true;
} else {
if (initData) {
window.FB.init(initData);
}
}
}
function runFacebookLogin () {
fbLogin.shouldRun = true;
replaceWindowOpen();
loginPopup();
enableFacebookSDK();
}
function replaceWindowOpen () {
const oldOpen = window.open;
window.open = function (url, name, windowParams) {
const u = new URL(url);
if (u.origin === 'https://www.facebook.com') {
name = popupName;
}
return oldOpen.call(window, url, name, windowParams);
};
}
function loginPopup () {
const width = Math.min(window.screen.width, 450);
const height = Math.min(window.screen.height, 450);
const popupParams = `width=${width},height=${height},scrollbars=1,location=1`;
window.open('about:blank', popupName, popupParams);
}
window.addEventListener('ddg-ctp-load-sdk', event => {
if (event.detail.entity === facebookEntity) {
enableFacebookSDK();
}
});
window.addEventListener('ddg-ctp-run-login', event => {
if (event.detail.entity === facebookEntity) {
runFacebookLogin();
}
});
window.addEventListener('ddg-ctp-cancel-modal', event => {
if (event.detail.entity === facebookEntity) {
fbLogin.callback({ });
}
});
// Instead of using fbAsyncInit, some websites create a list of FB API calls
// that should be made after init.
const bufferCalls = window.FB && window.FB.__buffer && window.FB.__buffer.calls;
function init () {
if (window.fbAsyncInit) {
siteInit = window.fbAsyncInit;
window.fbAsyncInit();
}
if (bufferCalls) {
for (const [method, params] of bufferCalls) {
if (Object.prototype.hasOwnProperty.call(window.FB, method)) {
window.FB[method].apply(window.FB, params);
}
}
}
}
if (!window.FB || window.FB.__buffer) {
window.FB = {
api: function (url, cb) { cb(); },
init: function (obj) {
if (obj) {
initData = obj;
runInit = true;
messageAddon({
appID: obj.appId
});
}
},
ui: function (obj, cb) {
if (obj.method && obj.method === 'share') {
const shareLink = 'https://www.facebook.com/sharer/sharer.php?u=' + obj.href;
window.open(shareLink, 'share-facebook', 'width=550,height=235');
}
// eslint-disable-next-line node/no-callback-literal
cb({});
},
getAccessToken: function () {},
getAuthResponse: function () {
return { status: '' };
},
// eslint-disable-next-line node/no-callback-literal
getLoginStatus: function (callback) { callback({ status: 'unknown' }); },
getUserID: function () {},
login: function (cb, params) {
fbLogin.callback = cb;
fbLogin.params = params;
messageAddon({
action: 'login'
});
},
logout: function () {},
AppEvents: {
EventNames: {},
logEvent: function (a, b, c) {},
logPageView: function () {}
},
Event: {
subscribe: function (event, callback) {
if (event === 'xfbml.render') {
callback();
}
},
unsubscribe: function () {}
},
XFBML: {
parse: function (n) {
parseCalls.push(n);
}
}
};
if (document.readyState === 'complete') {
init();
} else {
// sdk script loaded before page content, so wait for load.
window.addEventListener('load', (event) => {
init();
});
}
}
window.dispatchEvent(new CustomEvent('ddg-ctp-surrogate-load'));
console.warn('dispatched event');
})();
"""
broker?.push(method: "displayClickToLoadPlaceholders", params: ["ruleAction": ["block"]], for: self, into: webView)
webView.evaluateJavaScript(fbSurrogate, in: nil, in: WKContentWorld.page)
}
}
}
Original file line number Diff line number Diff line change
@@ -2,20 +2,24 @@
// function sendMessage (messageType, options, callback) {
// TODO chrome.runtime.sendMessage({ messageType, options }, callback)
// }

// LDA DISABLED
let appID
const loadingImages = {
darkMode: '',
lightMode: ''
}
let logoImg
const titleID = 'DuckDuckGoPrivacyEssentialsCTLElementTitle'
const titleID = 'DuckDuckGoPrivacyEssentialsCTLElementTitle_LDATEST_EMBED'
const entities = []
const entityData = {}
const fbSurrogate = `${fb-sdk.js}` // eslint-disable-line
const fbConfig = JSON.parse(`${clickToLoadConfig.json}`.replace(/\\/g, '')) // eslint-disable-line
const proximaRegFontInline = `${proximaRegFont}` // eslint-disable-line
const proximaBoldFontInline = `${proximaBoldFont}` // eslint-disable-line

return;

/**
*
* Base64 encode / decode
Original file line number Diff line number Diff line change
@@ -42,6 +42,7 @@
function enableFacebookSDK () {
if (!fbIsEnabled) {
window.FB = undefined
window.fbTest = "*** disabled surrogate ***";
window.fbAsyncInit = function () {
if (runInit && initData) {
window.FB.init(initData)
File renamed without changes.
Loading