مربعات الموافقة والإعلانات

نادرًا ما تأتي الصفحة المجلوبة من الويب وحدها: يغطيها مربع حوار للموافقة، وتحتها شريط لملفات تعريف الارتباط، وبين فقراتها إعلانات. افتراضيًا يطبع Weblitz ذلك كله، كما يفعل المتصفح. خياران يُسقطانه. كلاهما معطّل ما لم تضبطه، ولا ينقر أيٌّ منهما على شيء.

منذ WeblitzPdf ‎2.0.0-beta.4.

نوافذ الموافقة

يُسقط Consent = HtmlConsent.Hide نوافذ الموافقة من الطباعة. لا يُضغط أي زر ولا تُعطى أي إجابة باسم القارئ: تعمل الصفحة ومنصة الموافقة فيها كما تأتيان، ولا يُطبع ما تعرضانه منها. ويُخفي:

  • مربعات منصات الموافقة التي تستخدمها معظم المواقع (OneTrust و Sourcepoint و Cookiebot و Didomi و Usercentrics و CookieYes وغيرها)، عبر حاويات المنصات نفسها؛
  • ما تسمّيه EasyList Cookie List، وهي قائمة بإشعارات ملفات تعريف الارتباط لآلاف المواقع يحافظ عليها مؤلفو EasyList؛
  • مربع خاص بالصفحة: مثبّت على النافذة، أو مربع حوار، من بضعة أسطر، يتحدث عن ملفات تعريف الارتباط أو الموافقة أو الخصوصية ويحمل زرًا للإجابة؛
  • الغطاء الذي يضعه مثل هذا المربع على الصفحة، والقفل الذي يضعه على تمرير الصفحة، والذي كان سيقصر الطباعة على صفحتها الأولى.

يُذكر ما أُخفي في ملاحظات النتيجة (الرمز consentHidden)، ويُسمّى كل مربع خاص بالصفحة كما يسمّيه المحدِّد. أما المربع الذي لا تعرفه أي قاعدة فيبقى: سمّه في Hide. والموقع الذي لا يُظهر محتواه إلا بعد إعطاء الموافقة يطبع ما يُظهره من دونها.

الإعلانات وأدوات التتبع

يطبع Ads = HtmlAds.Block الصفحة كما يعرضها مانع الإعلانات، وفق قائمتي التصفية EasyList و EasyPrivacy:

  • لا يُرسل الطلب الذي تحظره القوائم: طلبات أقل، ونصوص برمجية أقل للتشغيل؛
  • لا يُطبع العنصر الذي تخفيه؛
  • الإطار أو الصورة الذي رُفض طلبه لا يترك مربعًا فارغًا.

تعدّ الملاحظات ما أُسقط (الرمز adsBlocked). يضيف AdFilters قواعد خاصة بك بصياغة القوائم، قاعدة في كل سطر: ||ads.example.com^ يحظر الطلبات إلى مضيف، وexample.com##.sponsored يُخفي عناصر في موقع، و@@ و#@# تضعان استثناءات. والموقع الذي يرفض القرّاء الذين يحظرون إعلاناته يطبع ما يعرضه عليهم.

القوائم، وإبقاؤها محدّثة

قوائم التصفية من عمل مؤلفي EasyList‏ (easylist.to). تحمل الحزمة لقطة من EasyList و EasyPrivacy و EasyList Cookie List ملفاتٍ نصية في lists/ بجانب تطبيقك، دون تغيير، مع إشعارها وتراخيصها في lists/NOTICE.txt. عمر اللقطة من عمر الحزمة، والقوائم تتغير كل يوم.

يجلب HtmlToPdf.UpdateFilterListsAsync() قوائم اليوم ويستخدمها من حينها. استدعه عند بدء تطبيقك:

var lists = await HtmlToPdf.UpdateFilterListsAsync();   // optional, at start

لا شيء سواه يجلب القوائم: لا يفعل ذلك أي عرض أبدًا، ولا يعمل شيء في الخلفية. تُحفظ النسخ في مجلد (مجلد خاص بك إن سمّيته) وتقرؤها عمليات التشغيل اللاحقة؛ لا تُجلب القائمة من جديد قبل انقضاء فترة التحديث الخاصة بها، والقائمة التي يتعذر جلبها تُبقي النسخة السابقة، أو اللقطة، قيد الاستخدام، مع السبب في النتيجة.

دون إعلانات وأدوات تتبّع

يطبع الخيار Ads = Block الصفحة كما يعرضها مانع الإعلانات، وفق قائمتي التصفية EasyList و EasyPrivacy (من عمل مؤلفي EasyList، وتحمل الحزمة نسخة منهما): لا يُرسل الطلب الذي تحظره القائمتان، ولا يُطبع العنصر الذي تخفيانه، ولا يترك الإطار الذي رُفض طلبه مربعًا فارغًا. ويضيف AdFilters قواعدك الخاصة بالصيغة نفسها، هنا للمربعات الممولة في الصفحة. وتحصي ملاحظة adsBlocked ما استُبعد. وهنا مع Consent = Hide أيضًا.

المرجع: HtmlPdfOptions.Ads، HtmlPdfOptions.AdFilters، HtmlToPdf.UpdateFilterListsAsync

الصفحة المطبوعة: news-page.html
<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><title>The Daily Example</title>
<style>
  body { font: 15px/1.6 Georgia, serif; margin: 0; color: #111827; }
  header { padding: 14px 32px; border-bottom: 3px double #111827; font: bold 26px Georgia, serif; }
  main { display: flex; gap: 32px; padding: 20px 32px; }
  article { flex: 1; }
  h1 { font-size: 26px; line-height: 1.25; margin: 0 0 8px; }
  .byline { color: #6b7280; font: 12px Arial, sans-serif; margin-bottom: 12px; }
  aside { width: 200px; flex: none; }
  .slot { display: block; background: #e5e7eb; color: #6b7280; font: 11px Arial, sans-serif; text-align: center; }
  .sponsored { margin-top: 16px; padding: 10px; border: 1px solid #d1d5db; font: 12px Arial, sans-serif; }
  .sponsored b { display: block; color: #6b7280; text-transform: uppercase; font-size: 10px; }
  #veil { position: fixed; inset: 0; background: rgba(17, 24, 39, .55); }
  #consent { position: fixed; top: 150px; left: 50%; width: 420px; margin-left: -230px; padding: 20px; background: #fff;
    border-radius: 8px; font: 13px/1.5 Arial, sans-serif; box-shadow: 0 8px 30px rgba(0, 0, 0, .3); }
  #consent h2 { margin: 0 0 8px; font-size: 17px; }
  #consent button { margin: 12px 8px 0 0; padding: 8px 14px; border: 0; border-radius: 4px; background: #111827; color: #fff; }
  #cookie-bar { position: fixed; left: 0; right: 0; bottom: 0; padding: 12px 32px; background: #111827; color: #fff;
    font: 13px Arial, sans-serif; }
  #cookie-bar a { color: #fbbf24; }
</style></head>
<body>
<header>The Daily Example</header>
<ins class="adsbygoogle slot" data-ad-slot="1234567890" style="height: 90px; line-height: 90px; margin: 12px 32px;">Advertisement</ins>
<main>
  <article>
    <h1>City council approves the new tram line after a decade of debate</h1>
    <div class="byline">By a staff reporter · 17 November 2026</div>
    <p>The council voted on Tuesday evening to build the line that will link the harbour to the northern districts.
    Work is to start in the spring and to last four years, with the first trams running on the southern half two years
    earlier.</p>
    <p>Supporters called the vote overdue. The line was first proposed in 2016 and has been redrawn three times since,
    each time to spare a street its trees or a square its market.</p>
    <iframe class="slot" src="https://securepubads.g.doubleclick.net/gampad/adx?slot=inline" width="300" height="250" style="border: 0; margin: 8px 0;"></iframe>
    <p>Opponents say the cost, now put at 480 million, will keep rising. The mayor answered that the city could not
    afford another decade of buses stuck in the same traffic as the cars they were meant to replace.</p>
    <p>The first works will close the harbour road at night from March.</p>
  </article>
  <aside>
    <div class="sponsored"><b>Sponsored</b>Ten gadgets every commuter needs this winter</div>
    <div class="sponsored"><b>Sponsored</b>The savings account the banks keep quiet about</div>
  </aside>
</main>
<div id="veil" hidden></div>
<div id="consent" role="dialog" aria-modal="true" hidden>
  <h2>We value your privacy</h2>
  We and our 214 partners use cookies and similar technologies to store and read information on your device, to show
  you personalised advertising and to measure its audience.
  <div><button>Accept all</button><button>Reject all</button><button>Manage preferences</button></div>
</div>
<div id="cookie-bar" hidden>We use cookies to give you the best experience. <a href="#">Find out more</a> · <a href="#" role="button">OK, I agree</a></div>
<script src="https://www.google-analytics.com/analytics.js" async></script>
<script>
  // (as a consent platform does: its boxes appear once the page has loaded)
  window.addEventListener('load', function () {
    for (const id of ['veil', 'consent', 'cookie-bar']) document.getElementById(id).hidden = false;
  });
</script>
</body></html>
الافتراضي: الصفحة كما هي
var result = HtmlToPdf.Render(html);
الصفحة 1 من ملف PDF الذي طبعته هذه الشيفرة
الصفحات
1
الملف
100,219 بايت (97.9 كيلوبايت) · ملف PDF
الملاحظات
  • Info resourceRefused: Script https://www.google-analytics.com/analytics.js: no resource loader was given (only data: URLs load).
  • Info resourceRefused: Frame https://securepubads.g.doubleclick.net/gampad/adx?slot=inline: no resource loader was given (only data: URLs load).
  • Info scriptsRan: The page's scripts ran (17 ms of virtual time, 2 tasks; 0 canvases drawn).
Ads = Block، مع قاعدة خاصة بنا
var result = HtmlToPdf.Render(html, new HtmlPdfOptions
{
    Ads = HtmlAds.Block,
    AdFilters = ["##.sponsored"],
    Consent = HtmlConsent.Hide,
});
الصفحة 1 من ملف PDF الذي طبعته هذه الشيفرة
الصفحات
1
الملف
52,292 بايت (51.1 كيلوبايت) · ملف PDF
الملاحظات
  • Info consentHidden: Consent boxes are not printed (Consent = Hide): the page's own div#consent, the page's own div#cookie-bar.
  • Info scriptsRan: The page's scripts ran (33 ms of virtual time, 6 tasks; 0 canvases drawn).
  • Info adsBlocked: Advertisements and trackers are left out (Ads = Block): 2 requests not made, 168 of the lists' hiding rules given to the page.